0

我有一个这样的字符串:

Error=0,0<br>Federal withhold=1.00<br>FICA=0.00<br>Medicare=0.00<br>Federal Supplemental withhold=0.00<br>State withhold=361.32<br>State Supplemental withhold=0.00<br>City withhold=0.00<br>City Supplemental withhold=0.00<br>City Resident withhold=0.00<br>City Resident Supplemental withhold=0.00<br>County withhold=0.00<br>County Supplemental withhold=0.00<br>School withhold=0.00<br>School Supplemental withhold=0.00<br>SDI withhold=0.00<br>SDI employer withhold=0.00<br>SUI employer withhold=0.00<br>Version=2013.01,1.02<br>No messages

从这个字符串中我想提取它的值State withhold实际上是361.32

我在 c# 字符串中有这个字符串,我已经尝试过IndexOfSubstring无法获得如何获得该值,直到<br>在那个特定单词之后State withhold

4

5 回答 5

5

使用正则表达式State withhold=([^<]+)<并从中读取值Match().Captures[1]

http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx

于 2013-10-31T11:04:07.183 回答
1
String s = "Error=0,0<br>Federal withhold=1.00<br>FICA=0.00<br>Medicare=0.00<br>Federal Supplemental withhold=0.00<br>State withhold=361.32<br>State Supplemental withhold=0.00<br>City withhold=0.00<br>City Supplemental withhold=0.00<br>City Resident withhold=0.00<br>City Resident Supplemental withhold=0.00<br>County withhold=0.00<br>County Supplemental withhold=0.00<br>School withhold=0.00<br>School Supplemental withhold=0.00<br>SDI withhold=0.00<br>SDI employer withhold=0.00<br>SUI employer withhold=0.00<br>Version=2013.01,1.02<br>No messages ";

var result = s.Split(new string[] { "<br>" }, StringSplitOptions.None).Where(x=>x.Split('=')[0]=="State withhold").Select(x=>x.Split('=')[1]);
于 2013-10-31T11:04:03.700 回答
1
decimal value = decimal.Parse(
                input.Split("<br>")
                .Where(x => x.StartsWith("State withhold"))
                .First().Split('=').ToArray()[1]);
于 2013-10-31T11:13:42.800 回答
0

您可以检查 achar是否为数字:char.IsDigit(c)

当您拥有“州扣缴”索引时,您可以从那里开始:

string afterThis = "State withhold";
int i = myString.IndexOf(afterThis) + afterThis.Length;
string tmpString = String.Empty;
while(char.IsDigit(myString.charAt(i)) || char.Equals('.'))
{
    tmpString += myString.charAt(i); // save it to a temp string
    i++;
}
double value = double.Parse(tmpString);
于 2013-10-31T11:05:11.680 回答
0

The following Regex should help...

(?<=State\swithhold=).*?(?=\<br\>)
于 2013-10-31T17:14:56.220 回答