Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing prefixes and suffixes from words in C#

Tags:

c#

c#-4.0

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.

like image 787
Jone Mamni Avatar asked May 07 '12 11:05

Jone Mamni


People also ask

How do you remove a suffix from a string?

There are multiple ways to remove whitespace and other characters from a string in Python. The most commonly known methods are strip() , lstrip() , and rstrip() . Since Python version 3.9, two highly anticipated methods were introduced to remove the prefix or suffix of a string: removeprefix() and removesuffix() .

How do I find the prefix and suffix of a string?

A prefix of a string S is a substring of S that occurs at the beginning of S. A suffix of a string S is a substring that occurs at the end of S. Output the size of the largest such subset. Note that the chosen subset can have a single string also.


1 Answers

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;
        }
like image 124
Michael Freidgeim Avatar answered Sep 29 '22 21:09

Michael Freidgeim