Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Replacing part of string by start and end position

Tags:

string

c#

replace

I have a string and I want to replace a part of it. The tricky part is that that I can't use Regex.replace, because I only know the start and end positions of the data in the string. For example, if the string looks like this:

I love cats, some more stuff here, we dont know how much more

And I have start=8 and end=11. And I want to replace that part to whatever I need to. This time lets say dogs so the new string will look like:

I love dogs, some more stuff here, we dont know how much more

How I could do that?

like image 409
hs2d Avatar asked Jun 27 '11 18:06

hs2d


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

ReplaceAt(int index, int length, string replace)

Here's an extension method that doesn't use StringBuilder or Substring. This method also allows the replacement string to extend past the length of the source string.

//// str - the source string
//// index- the start location to replace at (0-based)
//// length - the number of characters to be removed before inserting
//// replace - the string that is replacing characters
public static string ReplaceAt(this string str, int index, int length, string replace)
{
    return str.Remove(index, Math.Min(length, str.Length - index))
            .Insert(index, replace);
}

When using this function, if you want the entire replacement string to replace as many characters as possible, then set length to the length of the replacement string:

"0123456789".ReplaceAt(7, 5, "Salut") = "0123456Salut"

Otherwise, you can specify the amount of characters that will be removed:

"0123456789".ReplaceAt(2, 2, "Salut") = "01Salut456789"

If you specify the length to be 0, then this function acts just like the insert function:

"0123456789".ReplaceAt(4, 0, "Salut") = "0123Salut456789"

I guess this is more efficient since the StringBuilder class need not be initialized and since it uses more basic operations. Hope this help

like image 154
Valynk Avatar answered Oct 21 '22 12:10

Valynk


Simplest way:

string replaced = original.Substring(0, start) + replacementText + 
                  original.Substring(end);

I had expected StringBuilder to have something which would do this, but I think you'd have to call Remove then Insert.

like image 23
Jon Skeet Avatar answered Oct 21 '22 12:10

Jon Skeet