gpt4 book ai didi

c# - 从 C# 中的单词中删除前缀和后缀

转载 作者:太空狗 更新时间:2023-10-29 22:23:43 24 4
gpt4 key购买 nike

我有一串单词,我想从每个单词中删除一些后缀和前缀(位于一个数组中),然后将词干化的单词存储回一个字符串中。请问有什么建议吗?提前致谢。

后缀和前缀加起来有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;
}

这可以与后缀一起使用,那么前缀呢?有没有一种快速的方法可以同时处理后缀和前缀?我的字符串太长了。

最佳答案

My StringHelper class具有(除其他外)方法 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;
}

关于c# - 从 C# 中的单词中删除前缀和后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10480893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com