我正在尝试删除字符串中第一个空格后的所有内容,现在我已经尝试过了,但它确实无效。
我尝试了什么?
List<string> lines = new List<string>();
foreach (string textLine in richTextBox1.Lines)
{
lines.Add(textLine.Substring(0, textLine.IndexOf(" ") + 1));
}
richTextBox1.Text = "";
foreach (string line in lines)
{
richTextBox1.Text += line + Environment.NewLine;
}
字符串示例:
some:kind of string that I want totrim please.
现在......它应该在“some:kind”之后删除所有内容,为什么不呢?它只是在每一行的末尾修剪角色的出现,而且只修改一次。
我想分割的内容(代理列表)
83.71.175.121:8080 HTTP NOA Ireland 30% (9) + 11-jul-2017 21:06
188.120.209.97:53281 HTTPS HIA Czech Republic 70% (14) + 11-jul-2017 21:05
41.87.86.51:3128 HTTP NOA Nigeria 64% (9) + 11-jul-2017 21:05
187.84.222.153:80 HTTP ANM Brazil 61% (160) + 11-jul-2017 21:04
36.78.131.82:3128 HTTP NOA Indonesia 25% (49) + 11-jul-2017 21:02
181.199.202.248:8080 HTTPS NOA Spain 38% (35) + 11-jul-2017 21:01
180.253.231.211:8080 HTTPS NOA Indonesia new - 11-jul-2017 21:00
110.232.82.253:53695 SOCKS5 HIA Indonesia 60% (3) + 11-jul-2017 20:59
79.127.108.219:8080 HTTPS NOA Iran, Islamic Republic of 41% (9) + 11-jul-2017 20:58
189.219.24.155:16057 SOCKS5 HIA Mexico 64% (7) + 11-jul-2017 20:58
189.218.237.10:52009 SOCKS5 HIA Mexico 64% (7) + 11-jul-2017 20:58
118.173.67.181:8080 HTTP NOA Thailand new + 11-jul-2017 20:55
38.123.201.17:8080 HTTPS NOA United States 69% (25) + 11-jul-2017 20:55
81.128.165.5:3128 HTTP ANM United Kingdom 53% (59) + 11-jul-2017 20:54
64.137.185.193:8080 HTTP NOA Canada new + 11-jul-2017 20:54
103.76.12.118:8080 HTTPS NOA Indonesia 46% (75) + 11-jul-2017 20:54
209.141.47.120:80 HTTP ANM United States 58% (97) + 11-jul-2017 20:53
109.230.100.255:9090 HTTPS NOA Iran, Islamic Republic of 91% (135) - 11-jul-2017 20:52
180.250.174.251:8080 HTTPS NOA Indonesia 29% (40) - 11-jul-2017 20:52
180.251.81.2:8080 HTTPS HIA Indonesia new + 11-jul-2017 20:51
151.80.197.192:80 HTTP ANM France 50% (844) + 11-jul-2017 20:50
89.36.212.204:1189 HTTP NOA France 100% (8) + 11-jul-2017 20:47
109.230.230.209:3128 HTTP NOA Germany 100% (43) - 11-jul-2017 20:47
36.80.222.186:8080 HTTP NOA Indonesia new - 11-jul-2017 20:47
41.204.32.194:53281 HTTPS HIA Ghana 58% (15) - 11-jul-2017 20:46
188.195.53.120:8080 HTTP HIA Germany 50% (1) + 11-jul-2017 20:45
答案 0 :(得分:1)
执行string.Split()
:
string input = "some:kind of string that I want totrim please.";
input = input.Split()[0];
//input = "some:kind" now
我甚至将你的样本输入添加到我的小提琴中,它完美无瑕,小提琴here
答案 1 :(得分:1)
根据您的数据,问题在于您正在寻找空格,但这些值由制表符分隔。因此,您需要使用IndexOf('\t')
代替。或者,使用Split()[0]
的解决方案也可以工作,因为它将在包括制表符在内的任何空白区域中分割。
答案 2 :(得分:0)
尝试使用
lines.Add(textLine.Split(null)[0]);