简体   繁体   English

替换字符串中的字符

[英]replacing characters of a string

So I'm trying to iterate over a string and replace ever occurrence of a given substring with a new value. 因此,我试图遍历字符串,并用新值替换给定子字符串的任何出现。 I can't seem to figure out what the problem with my code is because it doesn't seem to make any changes to the strings i run through it. 我似乎无法弄清楚我的代码有什么问题,因为它似乎没有对我运行的字符串进行任何更改。

i create a new string nS that starts out as just “”, and am iterating through the template viewing each character as a substring s. 我创建了一个新字符串nS,它以“”开头,并遍历模板,将每个字符视为子字符串s。 In in every case that something needs to be replaced with a value i append said value on to the nS, else it just appends the current substring as is. 在每种情况下,都需要用一个值替换某些东西,我将所述值附加到nS上,否则它将仅按原样附加当前子字符串。

@Override
public String format(String template) {
    String nS = "";
    for (int i = 0, n = template.length(); i < n; i++) {
        String s = template.substring(i, i + 1);
        switch (s) {
            case "%%":
                nS = nS.concat("%");
                break;

            case "%t":
                nS = nS.concat(String.valueOf(inSeconds()));
                break;
       }
    }
    return nS;
}

the actual code has many more cases but i left them out so that its not as overwhelming. 实际的代码还有更多的情况,但我把它们排除在外,以至于不至于使人难以理解。

The ending index in the 2-arg substring method is exclusive . 2-arg substring方法中的结尾索引是Exclusive

The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. 子字符串从指定的beginIndex开始,并扩展到索引endIndex-1处的字符。

beginIndex - the beginning index, inclusive. beginIndex-起始索引(含)。

endIndex - the ending index, exclusive. endIndex-结束索引(不包括)。

You are getting a substring of exactly one character, not 2. Try i + 2 , after the appropriate bounds-checking: 您将得到一个仅一个字符而不是2个字符的子字符串。在进行适当的边界检查之后,尝试i + 2

String s = template.substring(i, i + 2);

Assuming performance is not a big issue I would do 假设性能不是我要解决的大问题

public String format(String template) {
    return template.replaceAll ("%%", "\uffff")
                .replaceAll("%t", ""+inSeconds())
                .replaceAll("\uffff", "%");
}

What you're describing attempting to do sounds like you're trying to rewrite String.replace() 您所描述的尝试执行的操作听起来像是您试图重写String.replace()

Given String s = "My Name Is Bob" 给定String s =“我的名字叫鲍伯”

and you would like to replace "Bob" with "Susan" all you need to do is: 并且您想用“ Susan”替换“ Bob”,您需要做的是:

String s = "My Name is Bob";
String n = s.replace("Bob", "Susan");
System.out.println(n); //My Name is Susan
System.out.println(s); //My Name is Bob

Another option, is to break the string into a character array and iterate over it. 另一种选择是将字符串分成一个字符数组并对其进行迭代。

String s = "My Name is Bob";
char[] bits = s.toCharArray();
for(char c : bits) {
   // logic
}

Compare two characters at once: 一次比较两个字符:

String s = "My Name is Bob";
char[] bits = s.toCharArray();
for(int i = 0; i < bits.length; i++) {
    if(i + 1 <= bits.length) {
       String searchFor = "" + bits[i] + bits[i + 1];
       // logic
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM