publicclassCharacterReplacement{publicstaticvoidmain(String[]args){Stringstr="Hello, World!";intindex=7;charnewChar='J';StringnewStr=replaceCharAtIndex(str,index,newChar);System.out.println(newStr);}publicstatic
System.out.println(myName); http://stackoverflow.com/questions/6952363/java-replace-a-character-at-a-specific-index-in-a-string
Stringstr="Hello, World!";StringBuildersb=newStringBuilder(str);sb.replace(7,8,"J");str=sb.toString();System.out.println("Replaced string: "+str); 1. 2. 3. 4. 5. 在上面的示例中,我们使用replace()方法将索引为7的字符替换为大写字母’J’,然后再将StringBuilder对象转换为字符串并输出结果。
System.out.println("Character at index 3: " + ch);// 根据字符串获取索引int index = buffer.indexOf("World"); System.out.println("Index of 'World': " + index); } } 3)StringBuilder方法 与StringBuffer基本一样的方法,但它不是线程安全。单线程中推荐使用。文档及使用代码可以参考上面StringBuffer。
public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int ...
Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n; otherwise, it is equal to the character at index k-n in the argument str. 参数: str - a string. 返回: a reference to this ...
Sets the character at the index. C# 複製 [Android.Runtime.Register("setCharAt", "(IC)V", "")] public override void SetCharAt(int index, char ch); Parameters index Int32 the zero-based index of the character to replace. ch Char the character to set. Attributes RegisterAttribute Exc...
For every nonnegative index k less than newLength, the character at index k in the new character sequence is the same as the character at index k in the old sequence if k is less than the length of the old character sequence; otherwise, it is the null character '\u0000'. In other ...
\n可以使用 String::format 、 String::replace 、 String::formatted 进行参数值替换其他方法:String:...
String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. String substring(int beginIndex) Returns a new string that is a substring of this strin...