Jone Mamni
Jone Mamni

Reputation: 223

Removing prefixes and suffixes from words in C#

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

The total number of suffixes and prefixes is more than 100, what the better to represent them? 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, how about the prefixes? Is there a fast way to do for both suffixes and prefixes at one time? My string is too long.

Upvotes: 6

Views: 17623

Answers (3)

Michael Freidgeim
Michael Freidgeim

Reputation: 28511

My StringHelper class has (among others) methods TrimStart, TrimEnd and StripBrackets, that can be useful for 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;
        }

Upvotes: 15

user1379561
user1379561

Reputation:

If you can't check from a dictionary what words are actually words then words like "premium" will be really hard to not mistake as a prefixed. Theoretically you could create somekind of rules used to check if the "mium" is an english word, but it would never be complete and would require a lot of work.

Upvotes: 0

Alejandro B.
Alejandro B.

Reputation: 5102

  1. Split your string into an array of stings, each entry being each word. To do this you use yourString.Split(','). Use the character separating your words in stead of ',', it may be a spcae ' '
  2. use a foreach to check if your words have any of the prefixes or sufixes, to do that you use yourWord.StartsWith("yourPrefix") and yourWord.EndsWith("yourPrefix")
  3. remove the prefix/suffix using yourWord.Replace or yourWord.SubString. Be carefull not to remove the prefix/sufixx if it's in the middle of the word!

Upvotes: 0

Related Questions