将一串字符串中第一次重复出现的字符找出

 1 public int FindFirstRepeadWord(String str) {
 2         //记录重复出现的位置
 3         int position = -1;
 4         int length = str.length();
 5         if (length == 1){
 6             return position;
 7         }
 8         for (int i = 0; i < length; i++) {
 9             //获取字符串的第一个字符
10             char c=str.charAt(i);
11             //查找c重复出现的位置,-1表示没有重复出现
12             int a = str.indexOf(c, i + 1);            
13             if (a != -1) {
14                 position = i;
15                 String s = str.substring(0,a);
16                 //继续深入判断是否还有重复的字符
17                 int k = FindFirstRepeadWord(s);
18                 if (k != -1)
19                     position = k;
20                 break;
21             }
22         }
23         return position;
24     }
原文地址:https://www.cnblogs.com/JDS20200528/p/13228929.html