使用replace的好像不行,会把所有数组中的字母全去掉删除一些is、the、a介词副词之类的string htmlstr = ""; string str = TextArea1.Value; str = str.Replace('.', ' '); str = str.Replace(',', ' '); str = str.Replace('?', ' '); str = str.Replace(';', ' '); str = str.Replace(':', ' '); str = str.Replace('!', ' '); string[] str1 = str.Split(' '); string[] value = str1;
2 回答

拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
这些词前或后必定有空格,你可以带上空格一起replace
str.replace(" a ","");
str.replace("a ","");
str.replace(" a","");

米琪卡哇伊
TA贡献1998条经验 获得超6个赞
把标点替换成空格,然后分隔空格成数组,再遍历删除?但是这样删除后原语句的标点和空格会无法还原到原来的位置。
用正则比较合适。
var str = TextArea1.Value;
var expr = "([ ]|.|,|?|;|:|!)(is|the|a)([ ]|.|,|?|;|:|!)";
MatchCollection collection = Regex.Matches(str, expr);
foreach (Match m in collection)
{
str = str.Replace(m.Value, "");
}
Console.WriteLine(str);
很久没用 c#,不知道写的对不对。
添加回答
举报
0/150
提交
取消