java java替换字符串特定索引中的子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13092406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java replace substring in string specific index
提问by user1569897
How would I replace a string 10100
with 10010
using the algorithm "replace the last substring 10 with 01."
I tried
我将如何替换字符串10100
与10010
使用算法“与01替换最后一个子10” 我试过
s=s.replace(s.substring(a,a+2), "01");
but this returns 01010
, replacing both the first and the second substring of "10"
.
"a" represents s.lastindexOf("10");
但这会返回01010
,替换 的第一个和第二个子字符串"10"
。"a" 代表 s.lastindexOf("10");
回答by acdcjunior
Here's a simple and extensible function you can use. First its use/output and then its code.
这是您可以使用的简单且可扩展的函数。首先是它的使用/输出,然后是它的代码。
String original = "10100";
String toFind = "10";
String toReplace = "01";
int ocurrence = 2;
String replaced = replaceNthOcurrence(original, toFind, toReplace, ocurrence);
System.out.println(replaced); // Output: "10010"
original = "This and This and This";
toFind = "This";
toReplace = "That";
ocurrence = 3;
replaced = replaceNthOcurrence(original, toFind, toReplace, ocurrence);
System.out.println(replaced); // Output: "This and This and That"
Function code:
功能代码:
public static String replaceNthOcurrence(String str, String toFind, String toReplace, int ocurrence) {
Pattern p = Pattern.compile(Pattern.quote(toFind));
Matcher m = p.matcher(str);
StringBuffer sb = new StringBuffer(str);
int i = 0;
while (m.find()) {
if (++i == ocurrence) { sb.replace(m.start(), m.end(), toReplace); break; }
}
return sb.toString();
}
回答by Rohit Jain
If you want to access the last two indices of a string, then you can use: -
如果要访问字符串的最后两个索引,则可以使用:-
str.substring(str.length() - 2);
This gives you string from index str.length() - 2
to the last character
, which is exactly the last two character.
这为您提供了从 indexstr.length() - 2
到 the 的字符串last character
,这正是最后两个字符。
Now, you can replace the last two indices with whatever string you want.
现在,您可以用您想要的任何字符串替换最后两个索引。
UPDATE: -
更新:-
Of you want to access the last occurrence of a character or substring, you can use String#lastIndexOf
method: -
如果要访问最后一次出现的字符或子字符串,可以使用String#lastIndexOf
方法:-
str.lastIndexOf("10");
Ok, you can try this code: -
好的,你可以试试这个代码:-
String str = "10100";
int fromIndex = str.lastIndexOf("10");
str = str.substring(0, fromIndex) + "01" + str.substring(fromIndex + 2);
System.out.println(str);
回答by Jim
You can get the last index of a character or substring using string's lastIndexOf method. See the documentation link below for how to use it.
您可以使用字符串的 lastIndexOf 方法获取字符或子字符串的最后一个索引。请参阅下面的文档链接以了解如何使用它。
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#lastIndexOf(java.lang.String)
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#lastIndexOf(java.lang.String)
Once you know the index of your substring, you can get the substring of all characters before that index, and the substring of all characters after the last character in your search string, and concatenate.
一旦知道子字符串的索引,就可以获取该索引之前所有字符的子字符串,以及搜索字符串中最后一个字符之后所有字符的子字符串,并进行连接。
This is a little drawn out, and I didn't actually run it (so I might have a syntax error), but it gives you the point of what I'm trying to convey at least. You could do this all in one line if you want, but it wouldn't illustrate the point as well.
这有点冗长,我实际上并没有运行它(所以我可能有语法错误),但它至少为您提供了我试图传达的重点。如果您愿意,您可以在一行中完成所有这些,但它也不能说明这一点。
string s = "10100";
string searchString = "10";
string replacementString = "01";
string charsBeforeSearchString = s.substring(0, s.lastIndexOf(searchString) - 1);
string charsAfterSearchString = s.substring(s.lastIndexIf(searchString) + 2);
s = charsBeforeSearchString + replacementString + charsAfterSearchString;
回答by Jim
10100 with 10010
10100 with 10010
String result = "10100".substring(0, 2) + "10010".substring(2, 4) + "10100".substring(4, 5);
回答by elias
The easiest way:
最简单的方法:
String input = "10100";
String result = Pattern.compile("(10)(?!.*10.*)").matcher(input).replaceAll("01");
System.out.println(result);