[英]Removing everything after the first " " in a string? (space)
I am trying to remove everything after the first space in a string, now I have already tried this but it really doesn't work.我正在尝试删除字符串中第一个空格之后的所有内容,现在我已经尝试过了,但它确实不起作用。
What did I try?我尝试了什么?
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;
}
String example:字符串示例:
some:kind of string that I want totrim please.
Now.. It SHOULD remove everything after "some:kind", why is it not?现在..它应该删除“some:kind”之后的所有内容,为什么不呢? It's only trimming the occurance of the character at the end of each line, and only once.
它只是在每行的末尾修剪字符的出现,而且只有一次。
What I am trying to split (proxy list)我要拆分的内容(代理列表)
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
Based on your data the issue is that you're looking for spaces, but the values are separated by tabs.根据您的数据,问题是您正在寻找空格,但这些值由制表符分隔。 So you need to use
IndexOf('\t')
instead.所以你需要使用
IndexOf('\t')
代替。 Alternatively the solution of using Split()[0]
would also work as that will split on any white space including tabs.或者,使用
Split()[0]
的解决方案也可以工作,因为它将在任何空白区域(包括制表符)上进行拆分。
尝试使用
lines.Add(textLine.Split(null)[0]);
Matching with a help of regular expresssion is yet another way:借助正则表达式进行匹配是另一种方式:
string input = "some:kind of string that I want totrim please.";
// @"^\s*\S*" if you want to ignore leading spaces
input = Regex.Match(input, @"^\S*").Value;
which can be helpful when we want to get rid of part after whitespace (space, tabulation, non-breaking space etc.)当我们想去掉空格之后的部分(空格、制表、不间断空格等)时,这会很有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.