[英]remove all occurrence of sub string from string other than first and last in java
is java have any option like remove all occurrence of a sub string from string other than 1st and last occurrence for example java是否有任何选项,例如从第一次和最后一次出现的字符串中删除所有出现的子字符串,例如
String sdata=" vijay is 10 yr old. vijay is studying in 5th grade. vijay is excellent in sports.vijay like cricket " String sdata =“ vijay已有10岁。vijay正在学习5年级。vijay在sports.vijay方面表现出色,例如板球”
i need a output like " vijay is 10 yr old. is studying in 5th grade. is excellent in sports.vijay like cricket " 我需要这样的输出:“ vijay已经10岁了。正在学习5年级。在运动中很出色。vijay像板球”
that means removing second and third occurrence of vijay. 这意味着消除了第二次和第三次发生的冲突。 does java have any inbuilt function for this?
java为此有任何内置函数吗?
This is a very specific requirement, so the answer is: No, Java does not have any standard function for this. 这是一个非常具体的要求,因此答案是:不,Java为此没有任何标准功能。
My recommendation: Use indexOf
and lastIndexOf
to find the first and the last occurence, split the string, use replaceAll
to get rid of all occurences in the middle part, and concatenate everything back together. 我的建议:使用
indexOf
和lastIndexOf
查找第一个和最后一个事件,拆分字符串,使用replaceAll
摆脱中间部分的所有事件,并将所有内容重新连接在一起。
If you can't solve this on your own, update the question with your code and your specific question/problem. 如果您不能独自解决此问题,请使用您的代码和特定的问题/问题来更新问题。
No it doesn't. 不,不是。 You could try something like this:
您可以尝试这样的事情:
String s = "vijay is 10 yr old. vijay is studying in 5th grade. vijay is excellent in sports.vijay like cricket";
String token = "vijay";
StringTokenizer st = new StringTokenizer(s,token);
int count = 0;
while ( st.hasMoreElements() )
{
String temp = st.nextToken();
if(count == 0 || count == (st.countTokens() - 1)
{
//your code
}else{
//your code
}
count ++;
}
As Jan say's it is quiet a specific thing. 正如Jan所说的,这是一件很安静的事情。 There are lots of Java methods and libraries but not that specific.
有很多Java方法和库,但没有具体说明。 So I've done you an example answer to show how you might go about this by iterating over the array, it does this.
因此,我为您提供了一个示例答案,以展示如何通过遍历数组来做到这一点,它可以做到这一点。
Removes all remaining occurrences of special character. 删除所有剩余的特殊字符。
private void start() { 私人void start(){
String origanlString = "vijay is 10 yr old. vijay is studying in 5th grade. vijay is excellent in sports. vijay like cricket."; String toFind = "vijay"; char deliminator = 'X'; // '\ ' char[] orig = origanlString.toCharArray(); char[] find = toFind.toCharArray(); System.out.println("orig length:"+orig.length); System.out.println("find length:"+find.length+"\\n"); System.out.println("[0] ***"+String.valueOf(orig)+"***("+orig.length+")chars"); // 1. Iterate over the array to be searched for(int i=0;i<orig.length;i++){ // If letter found equals the first letter of the toFind array, then check the rest of the characters Boolean matchFound = true; if(find[0]==orig[i]){ wordCheckLoop: for(int x=0;x<find.length;x++){ if(orig[i+x]!=find[x]){ matchFound = false; break wordCheckLoop; } } // Replace found string with place holders if(matchFound){ for(int x=0;x<find.length;x++){ orig[i+x] = deliminator; } } } } System.out.println("[1] ***"+String.valueOf(orig)+"***("+orig.length+")chars"); // 2. Replace the first occurance int count = 0; Boolean firstInst = true; replaceFirst: for(int i=0;i<orig.length;i++){ try{ if(orig[i]==deliminator&&firstInst){ orig[i]=find[count++]; if(orig[i+1]!=deliminator){ firstInst = false; } } }catch(ArrayIndexOutOfBoundsException ex){ break replaceFirst; } } System.out.println("[2] ***"+String.valueOf(orig)+"***("+orig.length+")chars"); // 3. Replace the last occurance firstInst = true; count = find.length-1; replaceLast: for(int i=orig.length-1;i>0;i--){ try{ if(orig[i]==deliminator&&firstInst){ orig[i]=find[count--]; if(orig[i-1]!=deliminator){ firstInst=false; } } }catch(ArrayIndexOutOfBoundsException ex){ System.out.println("ArrayIndexOutOfBoundsException"); break replaceLast; } } System.out.println("[3] ***"+String.valueOf(orig)+"***("+orig.length+")chars"); // 4. Remove all remaining occurances of special charactor count = 0; for(int i=0;i<orig.length;i++){ if(orig[i]==deliminator){ count++; } } char[] tempArray = new char[orig.length-count]; count = 0; for(int i=0;i<orig.length;i++){ if(orig[i]!=deliminator){ tempArray[count++] = orig[i]; } } System.out.println("[4] ***"+String.valueOf(tempArray)+"***("+tempArray.length+")chars"); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.