1 package com.duduli.li;
2 // 从一段字符串中找寻一个字符出现第几次的位置
3 // 在一段字符中,找出一个字的第几次出现位置
4 public class FindPlaceOfWord {
5 public int findNumber (String str,String letter,int num){
6 int i = 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 == num){
14 m = j;
15 break;
16 }
17 }
18 }
19 return m;
20 }
21 public static void main(String[] args) {
22 // TODO Auto-generated method stub
23 FindPlaceOfWord fpow = new FindPlaceOfWord();
24 String s = “JAVA 中文网:http://www.javaweb.cc/”;
25 int i = fpow.findNumber(s, “w”,4);
26 System.out.println(i);
27 System.out.println(s.substring(s.indexOf(“d”), i));
28 }
29
30 }
31
输出的结果应该是 www.java