Replacing a Char at a Given Index in 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);

Replacing a char at a given index in string?

Use a StringBuilder:

StringBuilder sb = new StringBuilder(theString);
sb[index] = newChar;
theString = sb.ToString();

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 one character at a specific index in a string

"So for a more concrete example, this is what I have and what I want to do: ...

... So is there any other way I can do this, or help me understand why I'm getting a seg fault?"

//I want to do this
input.at(2) = "a"; //obviously this doesn't work

It doesn't work, because "a" actually provides a const char* pointer, and not a char as required by std::string::at().

Use a simple character literal, not a c-style string literal:

 input.at(2) = 'a';
// ^^^

"When I try input.insert(2,a);, and cout << input I get bb$bbbbb followed by a seg fault message on the next line."

Besides std::string::insert() doesn't provide replacement of a character at the given position, but inserts one or more characters there, you may have an uninitialized char* a passed here, causing a segfault. But to answer this part finally, you should specify what the variable a actually is in this context.

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)

How to replace a character in a string using index in clojure

Like almost everything commonly used in Clojure, strings are immutable, so you need to create a new string with the new character in place of the old at the desired location:

(defn replace-at [s idx replacement]
(str (subs s 0 idx) replacement (subs s (inc idx))))

> (replace-at "012345" 2 "x")
01x345

Haskell: How to replace a string's character at a given index?

You can improve it slighly by using replacement : strAfter, and using a safe tail function to prevent an error if the index is greater than the length of the string:

safeTail :: [a] -> [a]
safeTail [] = []
safeTail (_:xs) = xs

replaceCharAtIndex :: Int -> Char -> String -> String
replaceCharAtIndex index replacement str = strHead ++ replacement : safeTail strAfter
where (strHead, strAfter) = splitAt index str

One however should be careful: for negative indices, it will replace the first character of the string. For indices that are greater than the length of the string, it will append to the string. Both cases are not per se the desired behavior.

We can alter the behavior by checking for negative indexes, and matching on the empty tail:

replaceCharAtIndex :: Int -> Char -> String -> String
replaceCharAtIndex index replacement str
| index < 0 = … -- index located before the string
| (_:xs) <- strAfter = strHead ++ replacement : xs
| otherwise = … -- index located after the string.
where (strHead, strAfter) = splitAt index str

Here we thus have two s to fill in a result for these edge-cases.

How to remove single character from a String by index

You can also use the StringBuilder class which is mutable.

StringBuilder sb = new StringBuilder(inputString);

It has the method deleteCharAt(), along with many other mutator methods.

Just delete the characters that you need to delete and then get the result as follows:

String resultString = sb.toString();

This avoids creation of unnecessary string objects.



Related Topics



Leave a reply



Submit