简体   繁体   English

Java-替换字符串:charAt VS子字符串

[英]Java - replace string: charAt VS substring

I'm making a simple test, just removing a char from string. 我正在做一个简单的测试,只是从字符串中删除一个字符。 It goes like this: 它是这样的:

String str = "kitten";
    int i = 2;
    //substring version - works good 
    System.out.println(str.replaceFirst(str.substring(i, i+1), ""));
    //charAt (tried as regex):
    System.out.println(str.replaceFirst("[str.charAt(i)]", ""));
    //charAt (tried as char):
    System.out.println(str.replaceFirst("str.charAt(i)", ""));

Substring version works good, charAt works good only if i=1. 子字符串版本运行良好,仅当i = 1时charAt才能运行良好。 What is wrong here ? 这是怎么了

In your second and third snippet, you're replacing not the result of charAt() call, but "charAt(i)" string. 在第二段和第三段中,您不是要替换charAt()调用的结果,而是要替换"charAt(i)"字符串。 See, it is in quotes. 看,用引号引起来。 Also, charAt() returns char so you have to convert it to String before using. 另外, charAt()返回char因此您必须在使用前将其转换为String

Try this: 尝试这个:

System.out.println(str.replaceFirst("[" + String.valueOf(str.charAt(i)) + "]", ""));
System.out.println(str.replaceFirst(String.valueOf(str.charAt(i)), ""));

System.out.println(str.replaceFirst("str.charAt(i)", "")); System.out.println(str.replaceFirst(“ str.charAt(i)”,“”));

doesn't do what you think it does. 不按照您的想法去做。 It isn't looking for a character at i . 它不是在找i一个角色。 It is looking for the first instance that matches the regex pattern "str.charAt(i)". 它正在寻找与正则表达式模式“ str.charAt(i)”匹配的第一个实例。 Similar issues exist with your other "replaceFirst" implementation. 您的其他“ replaceFirst”实现也存在类似的问题。

That means that "strAchar(i)" matches "str.charAt(i)" but when i happens to equal 2, "i" does not match "str.charAt(i)". 这意味着“ strAchar(i)”与“ str.charAt(i)”匹配,但是当我碰巧等于2时,“ i”与“ str.charAt(i)”不匹配。 The stuff between the double quotes is not interpreted as Java code. 双引号之间的内容不会解释为Java代码。

System.out.println(str.replaceFirst("str.charAt(i)", ""));

This line will replace the string "str,charAt(i)" by "" (if it occurs) in the string str . 这条线将在字符串替换字符串“STR,的charAt(I)”用“”(如果发生的话) str You need to read more about replace() and charAt() here . 您需要在此处阅读有关replace()charAt() 信息

Using the first example, which you say "works good", I'd expect this output: 使用第一个示例,您说“效果很好”,我希望得到以下输出:

kiten 小猫

ktten ktten

kitten 小猫

str.substring(i+1) returns 't' (i+1th or "3rd" character). str.substring(i+1)返回't' (i + 1或“ 3rd”字符)。 You then pass this into str.replaceFirst which replaces the first occurrence of 't' with "", effectively erasing it. 然后,将其传递给str.replaceFirst ,它将用“”替换首次出现的“ t ”,从而有效擦除它。

What you are doing in the second one is weird: You are invoking replaceFirst with the regex "[str.charAt(i)]" which basically means "replace the first of any of the characters in the square brackets (except the round brackets I think" so you may as well be saying "match any of the characters a,A,c,h,i,r,s,t" (I alphabetised, removed duplicates and '(', ')' and '.'), and the first of these characters that matches "kitten" just so happens to be 'i' so that charachter is removed. 您在第二个命令中所做的很奇怪:您正在使用正则表达式“ [str.charAt(i)]”调用replaceFirst ,这基本上意味着“替换方括号中的任何字符中的第一个(除了我的圆括号之外,思考”,因此您也可能会说“匹配任何一个字符a,A,c,h,i,r,s,t”(我按字母顺序排列,删除了重复项以及'(',')'和'。)。 ,而与“小猫”匹配的第一个字符恰好是“ i”,因此删除了charachter。

The final example is looking for a complete match on the string "str.charAt(i)" which is of course nowhere to be found in "kitten" you may as well be searching for "dog". 最后一个示例是在字符串“ str.charAt(i)”上寻找一个完全匹配的字符串,当然,在“小猫”中找不到该字符串,您也可能正在搜索“狗”。

The following code is equivalent to what you have just done: 以下代码与您刚刚完成的工作等效:

String str = "kitten";
int i = 2;
// Eliminated redundant regex replacement:
System.out.println(new StringBuffer(str.substring(0, i)).append(str.substring(i+1)));
// Search for any of the characters in "str.substring(i)"
System.out.println(str.replaceFirst("[aAchirst]", ""));
// Search for non-matching string
System.out.println(str.replaceFirst("dog", ""));

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

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