How to Replace Part of String by Position

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.

How to replace part of a string by position

Another option is sub, if you're not certain that the first XXX will always start at position 10:

sub("XXX", "000", "Jun:2020,XXX/XXX|May:2020,035/XXX|Apr:2020,040/XXX|")
# [1] "Jun:2020,000/XXX|May:2020,035/XXX|Apr:2020,040/XXX|"

How can I replace part of string by position to the end of the string?

Following is the possible solution.

string url = "https://test.local.com/{version}/123456";
int versionIndex = url.IndexOf("{version}");
string output = url;
if(versionIndex >= 0)
{
output = url.Substring(0,versionIndex) + "v50.0";
}

Replace a substring at a particular position in a string in Java

I would not use any replace() method if you know the indexes of the substring you want to replace. The problem is that this statement:

str = str.replace(str.substring(someIndex, someOtherIndex), replacement);

first computes the substring, and then replaces all occurrences of that substring in the original string. replace doesn't know or care about the original indexes.

It's better to just break up the string using substring():

int i1 = str.indexOf("(");
int i2 = str.indexOf(")");

str = str.substring(0, i1+1) + "tom" + str.substring(i2);

Replace character at certain location within string

Try substr()

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

How to replace a string at a particular position

The following is one option:

var myString = "Mar 16, 2010 00:00 AM";

myString = myString.substring(0, 13) +
"12" +
myString.substring(15, myString.length);

Note that if you are going to use this to manipulate dates, it would be recommended to use some date manipulation methods instead, such as those in DateJS.

Replace part of string at specified position

For example with substring() calls and concatenation (+):

var msg="Hello world this is a question";var replaced=msg.substring(0,6)+"friends"+msg.substring(11);console.log(replaced);

Pandas: Replacing part of string in column by position

You can do it with:

  1. extract the first 2 digits
  2. transform them to string
  3. append 00
df['C'] = df['B'].map(str).str[:2] + "00"

Replacing Part of String at Position in Powershell

Many of the methods of the StringBuilder class return the same StringBuilder instance so you can chain multiple calls together. This is harmless in C#, but in PowerShell you need to explicitly capture these return values so they don't appear in your function output. You can either pipe them to Out-Null...

$strBuilder.AppendLine($mainstring) | Out-Null
$strBuilder.Remove($position, $replacement.Length) | Out-Null;
$strBuilder.Insert($position, $replacement) | Out-Null;

...or cast them to [Void]...

[Void] $strBuilder.AppendLine($mainstring)
[Void] $strBuilder.Remove($position, $replacement.Length);
[Void] $strBuilder.Insert($position, $replacement);

...or chain all of the calls (including .ToString()) together...

return $strBuilder.AppendLine($mainstring) `
.Remove($position, $replacement.Length) `
.Insert($position, $replacement) `
.ToString();


Related Topics



Leave a reply



Submit