1

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_

6
  • 1
    What is your expected output? Commented Apr 21, 2016 at 12:36
  • // Hello // helloworld // hello_sharp // hello_scharp_ Commented Apr 21, 2016 at 12:39
  • if you wnt to remove underscore you can you replace funtion like this s.Replace("_','') Commented Apr 21, 2016 at 12:39
  • world should be replace with sharp, Commented Apr 21, 2016 at 12:40
  • structure should stay as is. I dont want remove the underscore Commented Apr 21, 2016 at 12:42

4 Answers 4

3

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 '_'.

Sign up to request clarification or add additional context in comments.

6 Comments

this doesnt check for whole word
@user6235593: Sorry, it does since word should be wrapped in either \b (word border) or _
oh btw cant use Linq functions since I use .net2
@user6235593: I've added a special test to demonstrate that whole word is checked
@user6235593: you're welcome, next time, please, provide more tests (for us not to guess what you're looking for), and specify restrictions (i.e. .Net 2.0 and alike)
|
1

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

4 Comments

Counter example: "hello world" should be changed into "hello charp"
@DmitryBychenko where is that stated? The OP never said that and none of their examples have spaces so why should that be necessary to handle?
technically it's not clearly stated, but in the question's code there's attempt to use regular expression \b{0}\b which indicates the attempt of whole word replacement
Another counter examples: "worlds" should be preserved intact when "my-world-to-be" should be changed (if you accept the hypothesis of whole word replacement).
0

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.

1 Comment

Counter example: "helloworld" expected to be preserved intact, but "hellocharp" is returned; you can't just Replace
0

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

5 Comments

will it do helloworld to helloSharp ?
@debin : sorry what you mean
@un-lucky they are saying that this will turn helloworld into hellosharp, when the OP only wants the word world preceded by an underscore to be changed to sharp
Counter example: "helloworld" expected to be preserved intact, but "hellocharp" is returned
Replace function doesnt check of whole word

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.