Changing One Character in a String

Changing one character in a string

Don't modify strings.

Work with them as lists; turn them into strings only when needed.

>>> s = list("Hello zorld")
>>> s
['H', 'e', 'l', 'l', 'o', ' ', 'z', 'o', 'r', 'l', 'd']
>>> s[6] = 'W'
>>> s
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
>>> "".join(s)
'Hello World'

Python strings are immutable (i.e. they can't be modified). There are a lot of reasons for this. Use lists until you have no choice, only then turn them into strings.

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 one character in a string with multiple characters in C

There are several ways it can be done. One way is to tokenize the string first, to find out where the spaces are, for example by using strtok.

Then copy the different sub strings (words) one by one into a new string (character array), for example with strcat. For each string you copy, also copy a string "---".

The alternative is to just do all this manually without calling any string library functions.

Yes, you will need loops.

How to change 1 char in the string?

I found a solution in unsafe context:

    string str = "gg"; char c = 'H'; int index = 1;
fixed (char* arr = str) arr[index] = 'H';
Console.WriteLine(str);

It's so simple.
And in safe context:

    string str = "gg"; char c = 'H'; int index = 1;
GCHandle handle = GCHandle.Alloc(str, GCHandleType.Pinned);
IntPtr arrAddress = handle.AddrOfPinnedObject();
Marshal.WriteInt16(arrAddress + index * sizeof(char), c);
handle.Free();
Console.WriteLine(str);

Replacing multiple chars in a string with one character in Python

try

import re
input_string = '1;AA;;1234567;;some text;;some text;;;some text;some text;;;;;;1, 2, 3, 4, 5, 6;;;;;;;;;;;another text;;;;;;;;;;;;;'
print(re.sub(r";+", ";", input_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

Replacing a single character in a String

You almost did it, just add a counter in your loop:

int num2replace = keyboard.nextInt();
int count = 0;
for (int i = 0; i < bLength; i++) {
if (baseString.charAt(i) == char2replace.charAt(0)) {
count++;
if (count == num2replace){
baseString = baseString.substring(0, i) +
secondChar + baseString.substring(i + 1);
break;
}
}
if (char2replace.length() > 1) {//you need move this out of loop
System.out.println("Error you can only enter one character");
}


}
System.out.println(baseString);

How to replace one character with multiple characters c#

One possible approach is to write a for loop and check if the character is a * an keep track using a counter how many replacements have already been done.

Make use of the modulo % to see which character you need for the replacement and use a StringBuilder to add the characters.

For example

string s="*****\n***\n****";
StringBuilder sb = new StringBuilder();
string subj = "asd";
int counter = 0;
for (int i = 0; i < s.Length; i++) {
if (s[i] == '*') {
sb.Append(subj[counter % subj.Length]);
counter++;
continue;
}
sb.Append(s[i]);
}
Console.WriteLine(sb.ToString());

Result

asdas
das
dasd

C# demo



Related Topics



Leave a reply



Submit