[英]Split substring from string and count till next character
我有這樣的字符串..
2931 8795 012 00:24:14 06/03/13 10:03 O006129009857 ** 0 0
現在按我的要求,我必須分裂子像2931
, 06/03/13
和O006129009857
。我已經使用IndexOf
串的方法,盡快得到字符串的開始和結束指數為空的空間就這樣產生的代碼..
int startIndex = line.IndexOf(Calldate);
int endIndex = line.IndexOf(" ", startIndex);
int difference = endIndex - startIndex;
strSubstring = (startIndex + "," + difference);
我們可以看到endIndex我已經選擇了line.IndexOf(" ", startIndex);
現在,我想以下一個字符為例,如果我必須帶O006129009857
那么我必須數到**
或該位置可用的任何其他字符來計算endIndex
..
請幫我計算endIndex
直到下一個字符代替 。 請幫助我。謝謝。
string.Split函數將為您完成此操作:
string text = "2931 8795 012 00:24:14 06/03/13 10:03 O006129009857 ** 0 0";
char[] Delimiters = new char[]{' '};
var result = text.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
結果將是2931 8795 012 00:24:14 06/03/13 10:03 O006129009857 ** 0 0
你不能只使用string.Split(''); 返回由空格[“”]分隔的字符串數組?
string CallDate = "2931 8795 012 00:24:14 06/03/13 10:03 O006129009857 ** 0 0 ";
string[] parts = CallDate.Split(' ');
parts = parts.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
您的零件陣列看起來像
{"2931", "8795", "012", "00:24:14", "06/03/13", "10:03", "O006129009857", "**", "0", "0"}
編輯:要計算單詞之間的間隔,請刪除該行
parts = parts.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
你的數組看起來像
"2931", "8795", "", "012", "00:24:14", "06/03/13", "10:03", "O006129009857", "", "", "", "", "", "", "**", "", "", "", "", "0", "", "", "", "", "", "", "", "", "", "", "0", "", "", "", ""
每個單詞之間的空格數是每個非空字符串之間的空格數。 通過簡單的for循環遍歷數組,您可以計算空格(因此可以計算字符串中每個作品的位置。
不清楚您要問的是什么,但是如果我理解正確,您想計算字符串中特定索引與下一個非空格字符之間的數字。 以下代碼假定您想知道foo和bar之間的字符數,但是您知道開始索引。
string input = "foo bar";
int startIndex = 3;
string pattern = @"\S";
var regex = new Regex(pattern);
int endIndex = regex.Match(input, startIndex).Index;
int characterCount = endIndex - startIndex;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.