public class ReplaceCharAtIndex {
public static void main(String[] args) {
String ab = "This is A String";
int index = 8;
String newString = ab.substring(0, index) + 'a'+ ab.substring(index + 1);
System.out.println(newString);
}
}
public class ReplaceCharAtIndex {
public static void main(String[] args) {
String ab = "This is A String";
StringBuilder newString = new StringBuilder(ab);
newString.setCharAt(8, 'a');
System.out.println(newString);
}
}
public class ReplaceCharAtIndex {
public static void main(String[] args) {
String oldString = "This is an example strimg";
int index = 23;
char[] charArray = oldString.toCharArray();
charArray[index] = 'n';
String newString = String.valueOf(charArray);
System.out.println(newString);
}
}
public class ReplaceCharString {
public static void main(String[] args) {
String oldString1 = "My name is Sam & I am a software developer.";
String newString1 = oldString1.replace(" &", ",");
System.out.println(newString1);
}
}
public class ReplaceCharString {
public static void main(String[] args) {
String oldString1 = "I have used multiple Internet providers & but my current provider is AT&T.";
String newString1 = oldString1.replaceFirst(" &", ",");
System.out.println(newString1);
}
}
输出:
I have used multiple Internet providerss, but my current provider is AT&T.
public class ReplaceCharString {
public static void main(String[] args) {
String oldString1 = "My name is Sam and I am a Software DeVeloper,";
String newString1 = oldString1.replace("V", "v").replace(",", ".");
System.out.println(newString1);
}
}
public class ReplaceAllChars {
public static void main(String[] args) {
String stringUnderscoresForward = "j_u_s_t_a_s/t/r/i/n/g";
String stringWithDigits = "abcd12345efgh";
String stringWithWhiteSpaces = "s t r i n g";
String stringWithLowerCase = "This is a Lower Case String";
String finalString1 = stringUnderscoresForward.replaceAll("[_/]", "-");
String finalString2 = stringWithDigits.replaceAll("[\\d]", "");
String finalString3 = stringWithWhiteSpaces.replaceAll("[ ]", "");
String finalString4 = stringWithWhiteSpaces.replaceAll("[\\s]", "-");
String finalString5 = stringWithLowerCase.replaceAll("[\\p{Lower}]", "");
System.out.println("Old String: "+stringUnderscoresForward+" New String: "+finalString1);
System.out.println("Old String: "+stringWithDigits+" New String: "+finalString2);
System.out.println("Old String: "+stringWithWhiteSpaces+" New String: "+finalString3);
System.out.println("Old String: "+stringWithLowerCase+" New String: "+finalString4);
System.out.println("Old String: "+stringWithLowerCase+" New String: "+finalString5);
}
}
输出:
Old String: j_u_s_t_a_s/t/r/i/n/g --New String: j-u-s-t-a-s-t-r-i-n-g
Old String: abcd12345efgh --New String: abcdefgh
Old String: s t r i n g --New String: string
Old String: This is a Lower Case String --New String: s-t-r-i-n-g
Old String: This is a Lower Case String --New String: T L C S
<script>
let string = "Delft stack is a good website to learn programming"
let result = string.replace("good","best")
console.log("original string: "+string)
console.log("updated string: "+result)
</script>
输出:
"original string: Delft stack is a good website to learn programming"
"updated string: Delft stack is a best website to learn programming"
<script>
let string = "Delft,stack,is,a,best,website,to,learn,programming"
let resultSingle = string.replace(","," ") //replace single
let resultAll = string.replace(/,/g," ") //replace all
console.log("Original string: "+string)
console.log("Replace single: "+resultSingle)
console.log("Replace All: "+resultAll)
</script>
输出:
"Original string: Delft,stack,is,a,best,website,to,learn,programming"
"Replace single: Delft stack,is,a,best,website,to,learn,programming"
"Replace All: Delft stack is a best website to learn programming"
let message = "This is a dummy text that we want to replace using replace function.";
let new_message = message.split("want").join("do not want");
console.log(new_message);
输出:
"This is a dummy text that we do not want to replace using replace function."
split() 函数在找到 want 的位置拆分 message 并返回两个子字符串 "This is a dummy text that we " 和 " to replace using replace function."。请记住,它在破坏字符串时不会删除空格。
join() 方法将这两个子字符串与 do not want 连接起来,并输出为 "This is a dummy text, we don't want to replace using replace function."。
下面的代码演示了每个函数如何操作并提供更详细的输出。
JavaScript 代码:
let message = "This is a dummy text that we want to replace using replace function.";
let split_message = message.split("want")
let new_message = split_message.join("do not want");
console.log(message);
console.log(split_message);
console.log(new_message);
输出:
"This is a dummy text that we want to replace using replace function."
["This is a dummy text that we ", " to replace using replace function."]
"This is a dummy text that we do not want to replace using replace function."
在 JavaScript 中复制 PHP str_replace() 函数来替换字符串
JavaScript 代码:
function str_replace($searchString, $replaceString, $message) {
// We create regext to find the occurrences
var regex;
// If the $searchString is a string
if ( typeof($searchString) == "string" ) {
// Escape all the characters used by regex
$searchString = $searchString.replace(/[.?*+^$[\]\\(){}|-]/g, "\\");
regex = new RegExp("(" + $searchString + ")", "g");
} else {
// Escape all the characters used by regex
$searchString = $searchString.map(function(i) {
return i.replace(/[.?*+^$[\]\\(){}|-]/g, "\\");
});
regex = new RegExp("(" + $searchString.join("|") + ")", "g");
}
// we create the replacement
var replacement;
// If the $searchString is a string
if ( typeof($replaceString) == "string" ) {
replacement = $replaceString;
} else {
// If the $searchString is a string and the $replaceString an array
if ( typeof($searchString) == "string" ) {
replacement = $replaceString[0];
} else {
// If the $searchString and $replaceString are arrays
replacement = function (i) {
return $replaceString[ $searchString.indexOf(i) ];
}
}
}
return $message.replace(regex, replacement);
}
let message = "This is a dummy text that we want to replace using replace function.";
console.log(str_replace("want", "do not want", message));
输出:
"This is a dummy text that we do not want to replace using replace function."