我有一串单词,我想从每个单词中删除一些后缀和前缀(位于数组中),然后将词干存储在字符串中.请问有什么建议吗?提前致谢.
后缀和前缀的总数超过100,表示它们的效果更好?阵列?正则表达式?请问有什么建议吗?
public static string RemoveFromEnd(this string str, string toRemove)
{
if (str.EndsWith(toRemove))
return str.Substring(0, str.Length - toRemove.Length);
else
return str;
}
Run Code Online (Sandbox Code Playgroud)
这可以使用后缀,前缀怎么样?有两种后缀和前缀的快速方法吗?我的字符串太长了.
MiF*_*vil 11
我的StringHelper类有(以及其他)TrimStart,TrimEnd和StripBrackets方法,对你有用
//'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;
}
Run Code Online (Sandbox Code Playgroud)
yourString.Split(','). 使用分隔单词的字符代替',',它可能是一个 spcae' 'yourWord.StartsWith("yourPrefix")和
yourWord.EndsWith("yourPrefix")