Replace Character at Certain Location Within String

Replace character at certain location within string

Try substr()

substr(s, 4, 4) <- "t"
> s
#[1] "test123"

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);

How to replace the certain character in certain position in the string?

I wonder if you can use a regex lookahead here to get what you are after.

str <- c("abcdccc","hijklccc","abcuioccc")
gsub("(^.{2})(?=c)(.*$)", "\\1X\\2", str, perl = T)

Or using a positive lookbehind as suggested by thelatemail

sub("(?<=^.{2})c", "X", str, perl = TRUE)

What this is doing is looking to match the letter c which is after any two characters from the start of the string. The c is replaced with X.

(?<= is the start of positive lookbehind

^.{2} means any two characters from the start of the string

)c is the last part which says it has to be a c after the two characters


[1] "abXcdccc"   "hijklccc"   "abXcuioccc"

If you want to read up more about regex being used (link)


Additionally a generalised function:

switch_letter <- function(x, letter, position, replacement) {
stopifnot(position > 1)

pattern <- paste0("(?<=^.{", position - 1, "})", letter)

sub(pattern, replacement, x, perl = TRUE)

}

switch_letter(str, "c", 3, "X")

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 part of string by position?

The easiest way to add and remove ranges in a string is to use the StringBuilder.

var theString = "ABCDEFGHIJ";
var aStringBuilder = new StringBuilder(theString);
aStringBuilder.Remove(3, 2);
aStringBuilder.Insert(3, "ZX");
theString = aStringBuilder.ToString();

An alternative is to use String.Substring, but I think the StringBuilder code gets more readable.

Python pandas replace string at specific location inside string

Try this:

frame['Corrected String'] = frame['String'].str[:4] + frame['Cavity'].str.split().str[-1] + frame['String'].str[5:]

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)



Related Topics



Leave a reply



Submit