现在我有:“山东分舵是.打分得分dfd fdfdsfds”我要截取字符“d”第二个到第四个之间的字符串。
以下代码可以实现
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 = "山东分舵是.打分得分dfd fdfdsfds";
29 int []i = fpowm.findNumber(s, "d",2,4);
30 System.out.println(s.substring(i[0], i[1]));
31 }
32
33 }
34