Java-将数字添加到匹配的单词中

Java-将数字添加到匹配的单词中,java,Java,我正在尝试为匹配的单词添加计数,如下所示: 匹配单词:“文本” 输入:文本文本示例文本 输出:Text1 text2text3text4text5示例text6 我试过这个: String text = "Text Text Text TextText ExampleText"; String match = "Text"; int i = 0; while(text.indexOf(match)!=-1) { text = text.replaceFir

我正在尝试为匹配的单词添加计数,如下所示:

匹配单词:“文本”

输入:文本文本示例文本

输出:Text1 text2text3text4text5示例text6

我试过这个:

String text = "Text Text Text TextText ExampleText";
String match = "Text";
int i = 0;
while(text.indexOf(match)!=-1) {
text = text.replaceFirst(match, match + i++);
}
不起作用,因为它将永远循环,匹配将保留在字符串中,IndexOf将永远不会停止

你建议我做什么?
有更好的方法吗?

试试这个解决方案是否经过测试

    String text = "Text Text Text TextText Example";
    String match = "Text";
    String lastWord=text.substring(text.length() -match.length());

    boolean lastChar=(lastWord.equals(match));

    String[] splitter=text.split(match);
    StringBuilder sb = new StringBuilder();
    for(int i=0;i<splitter.length;i++)
    {

       if(i!=splitter.length-1)
           splitter[i]=splitter[i]+match+Integer.toString(i);
       else
          splitter[i]=(lastChar)?splitter[i]+match+Integer.toString(i):splitter[i];

       sb.append(splitter[i]);
       if (i != splitter.length - 1) {
           sb.append("");
       }
    }
    String joined = sb.toString();
    System.out.print(joined+"\n");
String text=“文本示例”;
字符串匹配=“文本”;
字符串lastWord=text.substring(text.length()-match.length());
布尔lastChar=(lastWord.equals(match));
String[]splitter=text.split(匹配);
StringBuilder sb=新的StringBuilder();

对于(int i=0;i尝试测试此解决方案

    String text = "Text Text Text TextText Example";
    String match = "Text";
    String lastWord=text.substring(text.length() -match.length());

    boolean lastChar=(lastWord.equals(match));

    String[] splitter=text.split(match);
    StringBuilder sb = new StringBuilder();
    for(int i=0;i<splitter.length;i++)
    {

       if(i!=splitter.length-1)
           splitter[i]=splitter[i]+match+Integer.toString(i);
       else
          splitter[i]=(lastChar)?splitter[i]+match+Integer.toString(i):splitter[i];

       sb.append(splitter[i]);
       if (i != splitter.length - 1) {
           sb.append("");
       }
    }
    String joined = sb.toString();
    System.out.print(joined+"\n");
String text=“文本示例”;
字符串匹配=“文本”;
字符串lastWord=text.substring(text.length()-match.length());
布尔lastChar=(lastWord.equals(match));
String[]splitter=text.split(匹配);
StringBuilder sb=新的StringBuilder();

对于(int i=0;i这将适用于您:

public static void main(String[] args) {
    String s = "Text Text Text TextText ExampleText";
    int count=0;
    while(s.contains("Text")){
        s=s.replaceFirst("Text", "*"+ ++count); // replace each occurrence of "Text" with some place holder which is not in your main String.
    }
    s=s.replace("*","Text");
    System.out.println(s);


}
O/p:


这将对您有用:

public static void main(String[] args) {
    String s = "Text Text Text TextText ExampleText";
    int count=0;
    while(s.contains("Text")){
        s=s.replaceFirst("Text", "*"+ ++count); // replace each occurrence of "Text" with some place holder which is not in your main String.
    }
    s=s.replace("*","Text");
    System.out.println(s);


}
O/p:


一个可能的解决办法是

String text = "Text Text Text TextText ExampleText";
String match = "Text";
StringBuilder sb = new StringBuilder(text);
int occurence = 1;
int offset = 0;
while ((offset = sb.indexOf(match, offset)) != -1) {
    // fixed this after comment from @RealSkeptic
    String insertOccurence = Integer.toString(occurence);
    sb.insert(offset + match.length(), insertOccurence);
    offset += match.length() + insertOccurence.length();
    occurence++;
}
System.out.println("result: " + sb.toString());

一个可能的解决办法是

String text = "Text Text Text TextText ExampleText";
String match = "Text";
StringBuilder sb = new StringBuilder(text);
int occurence = 1;
int offset = 0;
while ((offset = sb.indexOf(match, offset)) != -1) {
    // fixed this after comment from @RealSkeptic
    String insertOccurence = Integer.toString(occurence);
    sb.insert(offset + match.length(), insertOccurence);
    offset += match.length() + insertOccurence.length();
    occurence++;
}
System.out.println("result: " + sb.toString());

我将@DeveloperH的代码重构为:

public class Snippet {

    public static void main(String[] args) {
        String matchWord = "Text";
        String input = "Text Text Text TextText ExampleText";
        String output = addNumbersToMatchingWords(matchWord, input);
        System.out.print(output);
    }

    private static String addNumbersToMatchingWords(String matchWord, String input) {
        String[] inputsParts = input.split(matchWord);

        StringBuilder outputBuilder = new StringBuilder();
        int i = 0;
        for (String inputPart : inputsParts) {
            outputBuilder.append(inputPart);
            outputBuilder.append(matchWord);
            outputBuilder.append(i);
            if (i != inputsParts.length - 1)
                outputBuilder.append(" ");
            i++;
        }
        return outputBuilder.toString();
    }
}

我将@DeveloperH的代码重构为:

public class Snippet {

    public static void main(String[] args) {
        String matchWord = "Text";
        String input = "Text Text Text TextText ExampleText";
        String output = addNumbersToMatchingWords(matchWord, input);
        System.out.print(output);
    }

    private static String addNumbersToMatchingWords(String matchWord, String input) {
        String[] inputsParts = input.split(matchWord);

        StringBuilder outputBuilder = new StringBuilder();
        int i = 0;
        for (String inputPart : inputsParts) {
            outputBuilder.append(inputPart);
            outputBuilder.append(matchWord);
            outputBuilder.append(i);
            if (i != inputsParts.length - 1)
                outputBuilder.append(" ");
            i++;
        }
        return outputBuilder.toString();
    }
}

下面是一个带有
StringBuilder
但无需拆分的:

public static String replaceWithNumbers( String text, String match ) {
    int matchLength = match.length();
    StringBuilder sb = new StringBuilder( text );

    int index = 0;
    int i = 1;
    while ( ( index = sb.indexOf( match, index )) != -1 ) {
        String iStr = String.valueOf(i++);
        sb.insert( index + matchLength, iStr );

        // Continue searching from the end of the inserted text
        index += matchLength + iStr.length();
    }

    return sb.toString();
}

下面是一个带有
StringBuilder
但无需拆分的:

public static String replaceWithNumbers( String text, String match ) {
    int matchLength = match.length();
    StringBuilder sb = new StringBuilder( text );

    int index = 0;
    int i = 1;
    while ( ( index = sb.indexOf( match, index )) != -1 ) {
        String iStr = String.valueOf(i++);
        sb.insert( index + matchLength, iStr );

        // Continue searching from the end of the inserted text
        index += matchLength + iStr.length();
    }

    return sb.toString();
}

首先获取一个stringbuffer,即result,然后将源与匹配项(destination)分开。 它会产生一系列空格和除“Text”之外的剩余单词。 然后检查isempty的条件,并根据该条件替换阵列位置

String text = "Text Text Text TextText ExampleText";
    String match = "Text";
    StringBuffer result = new StringBuffer();
    String[] split = text.split(match);
    for(int i=0;i<split.length;){
        if(split[i].isEmpty())
            result.append(match+ ++i);
        else
            result.append(split[i]+match+ ++i);
    }
    System.out.println("Result is =>"+result);
String text=“text ExampleText”;
字符串匹配=“文本”;
StringBuffer结果=新的StringBuffer();
String[]split=text.split(匹配);

对于(int i=0;i
Text1 Text2 Text3 Text4Text5示例text6

首先获取一个stringbuffer,即结果,然后将源与匹配项(目标项)分开。 它会产生一系列空格和除“Text”之外的剩余单词。 然后检查isempty的条件,并根据该条件替换阵列位置

String text = "Text Text Text TextText ExampleText";
    String match = "Text";
    StringBuffer result = new StringBuffer();
    String[] split = text.split(match);
    for(int i=0;i<split.length;){
        if(split[i].isEmpty())
            result.append(match+ ++i);
        else
            result.append(split[i]+match+ ++i);
    }
    System.out.println("Result is =>"+result);
String text=“text ExampleText”;
字符串匹配=“文本”;
StringBuffer结果=新的StringBuffer();
String[]split=text.split(匹配);

对于(int i=0;i
Text1 Text2 Text3 Text4Text5示例text6

我们可以通过使用stringbuilder解决这个问题,它提供了在字符串中插入字符的最简单构造。下面是代码

    String text = "Text Text Text TextText ExampleText";
    String match = "Text";
    StringBuilder sb = new StringBuilder(text);
    int beginIndex = 0, i =0;
    int matchLength = match.length();
    while((beginIndex = sb.indexOf(match, beginIndex))!=-1) {
         i++;
         sb.insert(beginIndex+matchLength, i);
         beginIndex++;
    }
    System.out.println(sb.toString());

我们可以通过使用stringbuilder来解决这个问题,它提供了在字符串中插入字符的最简单构造

    String text = "Text Text Text TextText ExampleText";
    String match = "Text";
    StringBuilder sb = new StringBuilder(text);
    int beginIndex = 0, i =0;
    int matchLength = match.length();
    while((beginIndex = sb.indexOf(match, beginIndex))!=-1) {
         i++;
         sb.insert(beginIndex+matchLength, i);
         beginIndex++;
    }
    System.out.println(sb.toString());


考虑使用两个字符串,一个用于原始数据,另一个用于修改。可能的改进:使用String Bu建器,使用正则表达式。创建第二个字符串,在其中写入已经调整的部分。。然后,将其从原始字符串中删除,以便while indexof最终返回-1。可以考虑使用两个字符串,一个用于原始数据,一个用于修改。可能的改进:使用StringBuilder,使用正则表达式。创建第二个字符串,在其中写入已“调整”的部分。然后,将其从原始字符串中删除,以便while indexof最终返回-1。如果文本中有*怎么办?您应该使用另一个符号,如Bell字符或smth。@Milkmaid-我正在编辑我的答案。您必须选择字符串中不存在的内容。如果文本中有*怎么办?您应该使用ano符号,如Bell字符或smth。@Milkmaid-我正在编辑我的答案。你必须选择字符串中不存在的内容。请给我新文本,然后测试它。要检查并修复issue@DeveloperH例如使用
text=“text ExampleTex”
match=“text”
结果是
Text0 Text1 Text2 Text3Text4 exampleText5
。使用我的解决方案,它就可以工作了。-)我刚刚完成问题。请给我新文本,然后测试它。要检查并修复issue@DeveloperH例如使用
text=“text ExampleTex”
match=“text”
结果是
Text0 Text1 Text2 Text3Text4 exampleText5
。使用我的解决方案,它就工作了。-)我刚刚完成了这个问题。很酷。它看起来像我的解决方案。;-)@次优可能是。我在发布之前已经测试过了,你可能已经发布了。顺便说一句,当要匹配的字符串是数字时,你测试过你的代码吗?很酷。这看起来像我的解决方案。;-@次优可能是。我在发布之前已经测试过了,你可能已经发布得更早了。顺便说一句,当要匹配的字符串是数字时,您是否测试了代码?做得不错。我编辑我的答案。请检查我的答案,然后编辑你的答案。我编辑我的答案。请检查我的答案,然后编辑你的