简体   繁体   中英

Remove a some words from a string based on text

I want to remove a name from a string. i have read so many article about it. They are suggesting to maintain an array list to maintain all the names as items. Is there any way to remove the name without maintain an array. I have a string as

string stVal="Naresh Jadapalli\r\nChandra shekhar ponnam\r\nBalu midhun gonu guntla";

Now I want to remove the name \\r\\nbalu midhun gonu guntla from the string. Thank you..

Assuming, that "balu midhun gonu guntla" is a typo, and it should be:

var strToRemove = "Balu midhun gonu guntla";

then the code looks like this:

stVal.Remove(stVal.IndexOf(strToRemove), strToRemove.Length);

If it isn't a typo, then just use another IndexOf overload:

stVal.Remove(stVal.IndexOf(strToRemove, StringComparison.CurrentCultureIgnoreCase), strToRemove.Length);

To do this properly you must account for the delimiters.

If you do not then you will have a bug if the name that you are replacing occurs as a substring of another name in the list.

For example, imagine if you were replacing "Matthew" inside this string:

"Matthew Smith\r\nMatthew\r\nMatthew Watson"

Presumably you would only want to replace the "\\r\\nMatthew\\r\\n" in the middle to give:

"Matthew Smith\r\nMatthew Watson"

But if you ignore the delimiters you would get this:

" Smith\r\nMatthew\r\nMatthew Watson"

The following method will remove the first match, accounting for the delimiters properly. Note that it also has to handle the special cases at the beginning and end of the string, because there is no delimiter at the start or end.

/// <summary>This removes the first occurrence  of the specified target string in the given text.</summary>
/// <param name="text">The text from which to remove the target string.</param>
/// <param name="target">The target string to remove.</param>
/// <param name="delimiter">The delimiter string used between potential target strings.</param>
/// <returns>The text with first occurrence  (if any) of the target string removed.</returns>

public static string RemoveFirstMatch(string text, string target, string delimiter)
{
    if (text == null)
        throw new ArgumentNullException("text");

    if (target == null)
        throw new ArgumentNullException("target");

    if (text.StartsWith(target + delimiter))
        return text.Substring(target.Length + delimiter.Length);

    if (text.EndsWith(delimiter + target))
        return text.Substring(0, text.Length - target.Length - delimiter.Length);

    return text.Replace(delimiter + target + delimiter, delimiter);
}

Sample test code:

string stVal="aaaa\r\naaab\r\naaab\r\naaac";
string delimiter = "\r\n";

Console.WriteLine(RemoveFirstMatch(stVal, "aaaa", delimiter) + "\n-------------");
Console.WriteLine(RemoveFirstMatch(stVal, "aaab", delimiter) + "\n-------------");
Console.WriteLine(RemoveFirstMatch(stVal, "aaac", delimiter) + "\n-------------");

You can also try this pattern:

(?i)Balu midhun gonu guntla(\r\n)*|(\r\n)+Balu midhun gonu guntla(\r\n)*$

See examples:

    string pattern = "(?i)Balu midhun gonu guntla(\r\n)*|(\r\n)+Balu midhun gonu guntla(\r\n)*$";

    string stVal = "Balu midhun gonu guntla\r\n\r\nNaresh Jadapalli\r\nNaresh Jadapalli";
    string res1 = Regex.Replace(stVal, pattern, "");

    stVal = "Naresh Jadapalli\r\n\r\nBalu midhun gonu guntla\r\n\r\nChandra shekhar ponnam\r\n\r\nBalu midhun gonu guntla\r\n\r\n";
    string res2 = Regex.Replace(stVal, pattern, "");

    stVal = "Naresh Jadapalli\r\n\r\nChandra shekhar ponnam\r\nChandra shekhar ponnam\r\n\r\nBalu midhun gonu guntla\r\n\r\n";
    string res3 = Regex.Replace(stVal, pattern, "");

simple solution for this to use regex find the requried occurances and replace them with required words

you can find good sample at C# RegEx Match

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM