JAVA截取字符串的第n个到第m个

1 package com.duduli.li;
2 //截断一个字符串,条件为某个字符的第n个位置和到第m个位置
3 public class FindPlaceOfWordMore {
4 public int [] findNumber (String str,String letter,int num1,int num2){
5 int i = 0;
6 int n = 0;
7 int m = 0;
8 char c = new String(letter).charAt(0);
9 char [] ch = str.toCharArray();
10 for(int j=0; j<ch.length; j++){
11 if(ch[j] == c){
12 i++;
13 if(i == num1){
14 n = j;
15 }
16 if(i == num2){
17 m = j;
18 break;
19 }
20 }
21 }
22 int [] size = {n,m};
23 return size;
24 }
25 public static void main(String[] args) {
26 // TODO Auto-generated method stub
27 FindPlaceOfWordMore fpowm = new FindPlaceOfWordMore();
28 String s = "JAVA中文网:http://www.javaweb.cc/";
29 int []i = fpowm.findNumber(s, "w",2,4);
30 System.out.println(s.substring(i[0], i[1]));
31 }
32
33 }
34

评论

暂无评论