user6235593
user6235593

Reputation: 95

replace string and ignore underscore

How to replace string and ignore underscore? the string structure should stay as is. I dont want remove the underscore. just replace the 'world' with 'sharp'. and only for whole words

string[] sentences =
{
    "Hello",
    "helloworld",
    "hello_world",
    "hello_world_"
};
foreach (string s in sentences)
{

    string pattern = String.Format(@"\b{0}\b", "world"); // whole case ignore underscore
    string result = Regex.Replace(s, pattern, "charp");

    Console.WriteLine(s + " = " + result);
}

output should be:

// Hello

// helloworld

// hello_charp

// hello_charp_

Upvotes: 1

Views: 1320

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186843

Something like this - in order to test for underscopes, but not include them into match use look ahead and look behind regex constructions.

  string[] sentences = new string[] {
     "Hello",
     "helloworld",
     "hello_world",
     "hello_world_",
     "hello, my world!", // My special test
     "my-world-to-be",   // ... and another one
     "worlds",           // ... and final one
  };

  String toFind = "world";
  String toReplace = "charp";

  // do to forget to escape (for arbitrary toFind String)
  string pattern = String.Format(@"(\b|(?<=_)){0}(\b|(?=_))", 
    Regex.Escape(toFind)); // whole word ignore underscore

  // Test:

  // Hello
  // helloworld
  // hello_charp
  // hello_charp_
  // hello, my charp!
  // my-charp-to-be
  // worlds

  foreach (String line in sentences)
    Console.WriteLine(Regex.Replace(line, pattern, toReplace));

In my solution I've assumed that you want to change whole words only which are separated by either word border ('\b') or underscope '_'.

Upvotes: 3

Alfie Goodacre
Alfie Goodacre

Reputation: 2793

You should use replace on _world

string[] sentences =
{
    "Hello",
    "helloworld",
    "hello_world",
    "hello_world_"
};
foreach (string s in sentences)
{

    string pattern = "_world";
    string result = s.Replace(pattern, "_charp");

    Console.WriteLine(s + " = " + result);
}

Just in case Dmitry is genuinely correct, it is worth also adding a second replace like so

    string pattern1 = "_world";
    string pattern2 = " world";
    string result = s.Replace(pattern1, "_charp").Replace(pattern2, " charp");

Upvotes: 1

sujith karivelil
sujith karivelil

Reputation: 29036

in the comments, you specify that "world should be replace with sharp," then Why not be a simple replace using .Replace()

string wordToReplace="world";
string replaceWith="charp";
string[] sentences = { "Hello", "helloworld", "hello_world", "hello_world_" };
foreach (string  item in sentences)
{
    Console.WriteLine(item.Replace(wordToReplace,replaceWith));
}

Upvotes: 0

NinjaDeveloper
NinjaDeveloper

Reputation: 1712

using string replace function see this code example

using System;

class Program
{
    static void Main()
    {
    const string s = "Dot Net Perls is about Dot Net.";
    Console.WriteLine(s);

    // We must assign the result to a variable.
    // ... Every instance is replaced.
    string v = s.Replace("Net", "Basket");
    Console.WriteLine(v);
    }
}

Output

Dot Net Perls is about Dot Net. Dot Basket Perls is about Dot Basket.

Upvotes: 0

Related Questions