简体   繁体   English

从java中的first和last以外的字符串中删除所有出现的子字符串

[英]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. 我的建议:使用indexOflastIndexOf查找第一个和最后一个事件,拆分字符串,使用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. 因此,我为您提供了一个示例答案,以展示如何通过遍历数组来做到这一点,它可以做到这一点。

  1. Goes through the char array and replaces an values that match the string you are searching. 遍历char数组并替换与您要搜索的字符串匹配的值。
  2. Places the first occurrence of the find string back in the main string. 将第一次出现的查找字符串放回主字符串中。
  3. Places the last occurrence of the find string back in the main string. 将最后一次出现的查找字符串放回主字符串中。
  4. 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.

相关问题 如何删除字符串Java中所有单词的出现 - How to remove all occurrence of word in string Java 查找从主字符串中删除单个出现的子字符串的方法 - Finding way to remove single occurrence of sub string from the main string 如何在 Java 中第一个和最后一个数字之间的所有数字出现之前立即向字符串添加字符? - How do I add characters to a string immediately before the occurrence of all numbers between the first and last ones in Java? 在Java中首次出现字符后,如何删除字符串的内容? - How to remove the contents of a string after the first occurrence of a character in java? 如何在Java中删除字符串中的第一个和最后一个字符? - How to remove first and last character in a string in Java? 如何在Java字符串中删除子字符串的首次出现? - How to remove the first occurance of a sub string in a string in java? 正则表达式以匹配字符串的第一个匹配项与最后一个匹配的字符串 - Regex to match first occurrence of a string is matching the last 用另一个字符串替换第一次和最后一次出现的字符串 - Replace first and last occurrence of a string with another 在最后一次出现字符后删除字符串 - Remove string after last occurrence of a character Java - 从字符串中第二个最后出现的字符获取 substring - Java - Get substring from 2nd last occurrence of a character in string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM