Java 在替换短语中的字符串时遇到问题

Java 在替换短语中的字符串时遇到问题,java,Java,我在替换短语中的某些字符串时遇到问题。我必须用和交替替换uu。到目前为止,我的代码是: private String line; //The line to format public TextFormatter(String lineToFormat) { line = lineToFormat; } /** * Finds the first single instance of str in line, starting at the postion start * <

我在替换短语中的某些字符串时遇到问题。我必须用交替替换uu。到目前为止,我的代码是:

private String line; //The line to format

public TextFormatter(String lineToFormat) {
    line = lineToFormat;
}

/**
 * Finds the first single instance of str in line, starting at the postion start
 * <p>
 * @param str   the string of length 1 to find. Guaranteed to be length 1.
 * @param start the position to start searching. Guaranteed to be in the string line.
 * <p>
 * @return the index of first instance of str if string found or -1 otherwise.
 */
private int findString(String str, int start) {
    String phrase = line;
    int psn = phrase.indexOf(str, start);
    return psn;
}

/**
 * Count the number of times single instances of str appear in the line.
 * <p>
 * @param str the string to find. Guaranteed to be length 1.
 * <p>
 * @return the number of times the string appears in the line.
 */
private int countStrings(String str) {
    int counter = 0;

    for (int i = 0; i < line.length(); i++) {
        if (line.charAt(i) == '_') {
            counter++;
        }
    }
    return counter;
}

/**
 * Replaces all single instances of underscores in the line given by line with italics tags. There must be an even
 * number of underscores in the line, and they will be replaced by <I>, </I>, alternating.
 * <p>
 * @return the line with single instances of underscores replaced with
 * <I> tags or the original line if there are not an even number of underscores.
 * <p>
 */
public String convertItalics() {
    String toReturn = " ";

    if (countStrings("_") % 2 == 0) { //makes sure there is even number of "_"
        for (int i = 0; i < line.length(); i++) {
            if (line.indexOf("_") + i == line.indexOf("_")) {
                toReturn += line.replace("_", "<I>");
            }
        }
    } else if (countStrings("_") % 2 != 0) {
        toReturn += line.replace("_", " ");
    }
    return toReturn;
}
我已经记下了第一个方法,但是我在转换斜体时遇到了麻烦。 如果我运行我的代码,我会让它替换u,但它不会替换

如果我有一个短语像hello,我的名字是chomez,它不能代替任何一个。任何帮助都将不胜感激。谢谢


编辑:多亏了那些留言的人,我的课快结束了,所以当我有时间使用电脑时,我会再回来看看,谢谢大家

我在这行上做了一个评论,我建议从一行创建一个字符串数组,并在该行上循环。 差不多

String [] words = line.split(" ");
然后循环这些词,称你的CountStringSwarks[i]


如果您想在Java中替换字符,我建议使用replace方法 例如:

 public class Test{
 public static void main(String args[]){
  String Str = new String("This is a long string");
  System.out.println(Str.replace('i', 'o'));
这将基本上用o代替i。此外,如果要在java中比较字符串,则必须使用equals或equalsIgnoreCase and not==方法,因为比较的是字符数和正确的顺序,而不是这些字符串的值


我希望这能帮助你

你在做屏幕报废吗?有很多方法可以做到这一点。正则表达式、字符串操作、堆栈等

这是最简单的方法之一

String code = "hello _my name is _chomez_";
code = code.replace("_", "<li>");
现在您已成功地将其转换为:

现在只需要做交替的结束标记

int n = code.indexOf("<li>", code.indexOf("<li>") + 1);
code = code.substring(0,n+1) + "/" + code.substring(n+1);
现在你有:


如果要处理3个以上的标记,只需运行一个循环,并将所有偶数位置标记替换为结束标记。算法将类似于,当有标记时,继续循环。如果这是第二、第四、第六。。即使是定位标记,也要将其替换为

以下代码完全符合您的要求:

public String convertItalics() 
{      
    String[] tags = {"</i>", "<i>"};
    StringBuilder builder = new StringBuilder(line);

    int matchIndex = 0, counter = 0;        

    while((matchIndex = builder.indexOf("_", matchIndex)) > 0)
    {           
        counter++;              
        int length = builder.length();
        int endIndex = matchIndex + 1;

        if(endIndex > length ||  endIndex == 0) endIndex = length;

        builder.replace(matchIndex, endIndex, tags[counter % 2]);               
    }

    return builder.toString();
}
希望这有帮助。祝你好运,编程愉快

干杯


崇高的

你应该使用现有的降价库。你可以考虑使用stacks@SLaks我是在课堂上学习的,所以我不确定这是什么,但我会查一下,谢谢。注意:用//***替换***并关闭convertItalic他想根据出现的次数替换字符。
int n = code.indexOf("<li>", code.indexOf("<li>") + 1);
code = code.substring(0,n+1) + "/" + code.substring(n+1);
hello <li>my name is </li>chomez<li>
public String convertItalics() 
{      
    String[] tags = {"</i>", "<i>"};
    StringBuilder builder = new StringBuilder(line);

    int matchIndex = 0, counter = 0;        

    while((matchIndex = builder.indexOf("_", matchIndex)) > 0)
    {           
        counter++;              
        int length = builder.length();
        int endIndex = matchIndex + 1;

        if(endIndex > length ||  endIndex == 0) endIndex = length;

        builder.replace(matchIndex, endIndex, tags[counter % 2]);               
    }

    return builder.toString();
}
hello <i>my name</i> is <i>chomez</i> <i>l