我有一個文本框,一個標籤和一個按鈕,文本框格式如下:如何刪除連字符「 - 」但計數之前刪除連字符在C#中?
23-44-33-32-34-05-40-12
我想,當用戶單擊該按鈕,然後刪除所有的「 - 」,但計算每個數字並存儲一些字符串變量,然後在標籤上顯示總數或字符串。
在此先感謝。
我有一個文本框,一個標籤和一個按鈕,文本框格式如下:如何刪除連字符「 - 」但計數之前刪除連字符在C#中?
23-44-33-32-34-05-40-12
我想,當用戶單擊該按鈕,然後刪除所有的「 - 」,但計算每個數字並存儲一些字符串變量,然後在標籤上顯示總數或字符串。
在此先感謝。
試試這個:
//Getting the textbox data and assigning to a string
string initialstring = textbox1.text;
//Calculating the total number of words available
int totalcount = initialstring.Split('-').Length;
//Replacing the '-' character which need to be reassign to textbox
string formattedstring = initialstring.Replace("-", " ");
//Assigning formatted data to textbox
textbox1.text = formattedstring;
對於如此簡單的事情來說工作太多。 – TheProvost 2015-01-21 07:30:35
它的工作原理,但如何在清除「 - 」之前統計所有數字? – 2015-01-21 07:44:13
@CeebLaj Thoj,這裏我計算刪除' - '之前的單詞數量。它返回計數爲8 – 2015-01-21 08:01:20
你可以做的是先更換字符,然後在其上做計數。你可以不喜歡以下:
var countOfCharacters = mystring.Replace("-", "").Length;
youlabel.Text = countOfCharacters;
很簡單:
var nrOfChars = yourTextBox.Text.Count(c => c != '-')
[與string.replace()](https://msdn.microsoft.com/en-us/library/fk49wtc1%28v= vs.110%29.aspx) – 2015-01-21 07:23:32
你是什麼意思_顯示總數_正好?你想得到總數爲'23 + 44 + ...',或者你想要在你刪除所有'-'字符後計算字符串中有多少數字? – 2015-01-21 07:25:21
CeebLaj Thoj - 查看我的回答和評論 – 2015-01-21 08:02:25