[英]Using Substring to get digit before a string
我正在用C#讀取一個文件。 我想從字符串中檢查值。 該行包括以下內容:
15 EMP_L_NAME HAPPENS 5 TIMES.
40 SUP HAPPENS 12 TIMES.
我想在字符串“TIMES”之前找到字符串中的次數。 我寫了以下代碼:
int arrayLength = 0;
int timesindex = line.IndexOf("TIMES");
if (timesindex > 0)
{
//Positon of the digit "5" in the first line
int indexCount = timesindex - 2;
if (int.TryParse(line.Substring(indexCount, 1), out occursCount))
{
arrayLength = occursCount;
}
}
使用上面的代碼,我可以找到單個digigt數的“TIMES”數。 但如果它是一個兩位數,它將無法工作(例如第二行)。 我必須開發一個邏輯來查找由“TIMES”空格分隔的數字。 我怎么能這樣做?
你可以做:
喜歡:
string str = "15 EMP_L_NAME HAPPENS 5 TIMES. ";
string[] array = str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int index = Array.IndexOf(array, "TIMES.");
int number;
if (!int.TryParse(array[index - 1], out number))
{
//invalid number
}
Console.WriteLine(number);
如果輸入是可靠的,你可以使用String.Split()
...
int arrayLength = 0;
int timesindex = line.IndexOf("TIMES");
if (timesindex > 0)
{
string[] items = line.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
if (int.TryParse(items[items.Length - 2], out occursCount))
{
arrayLength = occursCount;
}
}
該方法依賴於所需的數字,即每行中最后一個“單詞”的第二個數字
如果您的字符串始終是相同的格式,只有五個單詞或“部分”或您想要調用它們的任何內容,您可以使用:
int times = 0;
Int32.TryParse(line.Split(' ')[3], out times);
這必須更加健壯,字符串中可能不存在數字,或者字符串格式完全不同。
看看LastIndexOf結合你的時間timesindex
。 您可以在前面的空格之前查找空格( timesindex
-1),然后在數字周圍有兩個位置。
int timesindex = line.IndexOf("TIMES");
int firstSpace = line.LastIndexOf(" ", timesindex-1);
string number = line.Substring(firstSpace, timesindex-firstSpace);
雖然這可能需要對索引進行一些調整,但無論如何這都是想法
嘗試這個
int timesindex = line.IndexOf("TIMES");
int happensindex = line.IndexOf("HAPPENS") + 7; //Add 7 cause HAPPEND is 7 chars long
if (timesindex > 0)
{
//Positon of the digit "5" in the first line
if (int.TryParse(line.Substring(happensindex, timesindex).trim(), out occursCount))
{
arrayLength = occursCount;
}
}
您可以使用System.Text.RegularExpressions.Regex
,即正則表達式,以便在字符串中查找模式:
string input = "40 SUP HAPPENS 12 TIMES.";
Match match = Regex.Match(input, @"(?<=HAPPENS\s)\d+(?=\sTIMES)");
if (match.Success) {
Console.WriteLine(match.Value); '==> "12"
}
正則表達式的解釋:它使用通用模式(?<=prefix)find(?=suffix)
來查找前綴和后綴之間的位置。
(?<=HAPPENS\s) Prefix consisting of "HAPPENS" plus a whitespace (\s)
\d+ A digit (\d) repeated one or more times (+)
(?=\sTIMES) Suffix consisting of a whitespace (\s) plus "TIMES"
如果你只想測試“TIMES”而不是“HAPPENS”,你可以放棄第一部分:
Match match = Regex.Match(input, @"\d+(?=\sTIMES)");
由於您多次使用相同的搜索模式,建議您創建一次Regex
而不是調用靜態方法:
Regex regex = new Regex(@"\d+(?=\sTIMES)");
// Use many times with different inputs
Match match = regex.Match(input);
正則表達式會更清潔:
var regex = new Regex(@"(\d+)\sTIMES"); // match a number followed by whitespace then "TIMES"
string num = regex.Match(" 15 EMP_L_NAME HAPPENS 5 TIMES").Groups[1].ToString();
int val = int.Parse(num);
使用LINQ:
string[] lines = {"15 EMP_L_NAME HAPPENS 5,1 TIMES.", "40 SUP HAPPENS 12 TIMES. "};
var allValues = lines.Select(line =>
{
double temp;
var words = line.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
var value = words[Array.IndexOf(words,"TIMES.") - 1];
if (double.TryParse(value, out temp)) return temp;
else return 0;
}).ToList();
foreach (var value in allValues)
{
Console.WriteLine(value);
}
// Output:
// 5,1
// 12
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.