Removing prefixes and suffixes from words in C #

I have a string of words, and I want to remove from each word the number of suffixes and prefixes (which are located in the array), and then save back the words in the string. Is there any suggestion please? thanks in advance.

The total number of suffixes and prefixes is more than 100, what is better to represent them? An array? Regex? Is there any suggestion please?

public static string RemoveFromEnd(this string str, string toRemove)
{
if (str.EndsWith(toRemove))
    return str.Substring(0, str.Length - toRemove.Length);
else
    return str;
}

This can work with suffixes, what about prefixes? Is there a quick way to do this for both suffixes and prefixes at a time? My line is too long.

+5
source share
3 answers

My StringHelper class has (among other things) TrimStart, TrimEnd, and StripBrackets methods that may be useful to you.

//'Removes the start part of the string, if it is matchs, otherwise leave string unchanged
    //NOTE:case-sensitive, if want case-incensitive, change ToLower both parameters before call
    public static string TrimStart(this string str, string sStartValue)
    {
        if (str.StartsWith(sStartValue))
        {
            str = str.Remove(0, sStartValue.Length);
        }
        return str;
    }
    //        'Removes the end part of the string, if it is matchs, otherwise leave string unchanged
    public static string TrimEnd(this string str, string sEndValue)
    {
        if (str.EndsWith(sEndValue))
        {
            str = str.Remove(str.Length - sEndValue.Length, sEndValue.Length);
        }
        return str;
    }
//        'StripBrackets checks that starts from sStart and ends with sEnd (case sensitive).
//        'If yes, than removes sStart and sEnd.
//        'Otherwise returns full string unchanges
//        'See also MidBetween
        public static string StripBrackets(this string str, string sStart, string sEnd)
        {
            if (StringHelper.CheckBrackets(str, sStart, sEnd))
            {
                str = str.Substring(sStart.Length, (str.Length - sStart.Length) - sEnd.Length);
            }
            return str;
        }
+9
  • , . yourString.Split(','). , ',', spcae ' '
  • foreach, , - sufixes, yourWord.StartsWith("yourPrefix") yourWord.EndsWith("yourPrefix")
  • /, yourWord.Replace yourWord.SubString. , /sufixx, !
0

, , "" . somekind , , "mium" , .

0

All Articles