簡體   English   中英

刪除字符串中第一個“”之后的所有內容? (空間)

[英]Removing everything after the first " " in a string? (space)

我正在嘗試刪除字符串中第一個空格之后的所有內容,現在我已經嘗試過了,但它確實不起作用。

我嘗試了什么?

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

做一個string.Split()會更簡單:

string input = "some:kind of string   that I want      totrim please."; 
input = input.Split()[0];
//input = "some:kind" now

我什至將您的示例輸入添加到我的小提琴中,它完美地工作,小提琴在這里

根據您的數據,問題是您正在尋找空格,但這些值由制表符分隔。 所以你需要使用IndexOf('\t')代替。 或者,使用Split()[0]的解決方案也可以工作,因為它將在任何空白區域(包括制表符)上進行拆分。

嘗試使用

lines.Add(textLine.Split(null)[0]);

借助正則表達式進行匹配是另一種方式:

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; 

當我們想去掉空格之后的部分(空格、制表、不間斷空格等)時,這會很有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM