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? 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
It would be simpler to do a string.Split()
:
string input = "some:kind of string that I want totrim please.";
input = input.Split()[0];
//input = "some:kind" now
I even added your sample input to my fiddle and it works flawlessly, fiddle here
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. Alternatively the solution of using Split()[0]
would also work as that will split on any white space including tabs.
尝试使用
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.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.