How to Replace the Characters in a String

Replace a character at a specific index in a string?

String are immutable in Java. You can't change them.

You need to create a new string with the character replaced.

String myName = "domanokz";
String newName = myName.substring(0,4)+'x'+myName.substring(5);

Or you can use a StringBuilder:

StringBuilder myName = new StringBuilder("domanokz");
myName.setCharAt(4, 'x');

System.out.println(myName);

Java: Replace a specific character with a substring in a string at index

String[][] arr = {{"1", "one"}, 
{"5", "five"}};

String str = "String5";
for(String[] a: arr) {
str = str.replace(a[0], a[1]);
}

System.out.println(str);

This would help you to replace multiple words with different text.

Alternatively you could use chained replace for doing this, eg :

str.replace(1, "One").replace(5, "five");

Check this much better approach : Java Replacing multiple different substring in a string at once (or in the most efficient way)

Replacing instances of a character in a string

Strings in python are immutable, so you cannot treat them as a list and assign to indices.

Use .replace() instead:

line = line.replace(';', ':')

If you need to replace only certain semicolons, you'll need to be more specific. You could use slicing to isolate the section of the string to replace in:

line = line[:10].replace(';', ':') + line[10:]

That'll replace all semi-colons in the first 10 characters of the string.

How do I replace a character at a particular index in JavaScript?

In JavaScript, strings are immutable, which means the best you can do is to create a new string with the changed content and assign the variable to point to it.

You'll need to define the replaceAt() function yourself:

String.prototype.replaceAt = function(index, replacement) {
return this.substring(0, index) + replacement + this.substring(index + replacement.length);
}

And use it like this:

var hello = "Hello World";
alert(hello.replaceAt(2, "!!")); // He!!o World

How to replace the characters in a string

replacements = {
'i' => 'eye', 'e' => 'eei',
'a' => 'aya', 'o' => 'oha'}
word = "Cocoa!55"
word.gsub(Regexp.union(replacements.keys), replacements)
#⇒ "Cohacohaaya!55"

Regexp::union, String#gsub with hash.

Replacing all characters of a String with different ones

Just iterate over the character array representation of your input string and use each character's string representation, as a key, to fetch corresponding value from the translator map.

System.out.println("Enter a phrase : ");
char[] txt = scan.nextLine().toCharArray(); //it's more comfortable and faster to work with char array

for(char c : txt) {
System.out.print(translator.get(String.valueOf(txt[i]))+" "); //you need a string representation of char value, as your map's key is String
}

So, the input:

Grejuc Andrei

will print:

0100 0111 0111 0010 0110 0101 0110 1010 0111 0101 0110 0011 null 0100 0001 0110 1110 0110 0100 0111 0010 0110 0101 0110 1001

Pay attention! printed text includes null, and I leave this for you to identify - why.



Related Topics



Leave a reply



Submit