string str = {"Items":[{"Power":"402 W"},{"DailyYield":"5.3 kWh"},{"TotalYield":"8353.52 kWh"}]};
string[] para1 = str.split("W");//大写的W
string result = "";
if(para1 != null && para1.length > 0)
{
for(int i = 0;i < para1.length;i ++)
{
string[] para2= para1[i].split("\"");
if(para2 != null && para2.length > 0)
{
result += para2[para2.length - 1].replace("k","") + "#";
}
}
result = result.subString(0,result.length - 1);
}
我是这样来写的,最后的result和你要的结果一样。不知道能不能帮到你忙。
string[] para1 = str.split("W");//大写的W
string result = "";
if(para1 != null && para1.length > 0)
{
for(int i = 0;i < para1.length;i ++)
{
string[] para2= para1[i].split("\"");
if(para2 != null && para2.length > 0)
{
result += para2[para2.length - 1].replace("k","") + "#";
}
}
result = result.subString(0,result.length - 1);
}
我是这样来写的,最后的result和你要的结果一样。不知道能不能帮到你忙。
string res=null;
Match m;m=r.Match(sourcestring);while(m.Success){
res=m.Value+"#";
m=r.NextMatch;
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "{\"Items\":[{\"Power\":\"402 W\"},{\"DailyYield\":\"5.3 kWh\"},{\"TotalYield\":\"8353.52 kWh\"}]}";
var reg = new Regex(@"\d+(?:\.\d+)?");
string s = "";
Match ret = reg.Match(str);
while (ret.Success)
{
s += ret.Value + "#";
ret = ret.NextMatch();
}
s = s.Remove(s.Length - 1);
Console.WriteLine(s);
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "{\"Items\":[{\"Power\":\"402 W\"},{\"DailyYield\":\"5.3 kWh\"},{\"TotalYield\":\"8353.52 kWh\"}]}";
var reg = new Regex(@"\d+(?:\.\d+)?");
string s = "";
Match ret = reg.Match(str);
while (ret.Success)
{
s += ret.Value + "#";
ret = ret.NextMatch();
}
s = s.Remove(s.Length - 1);
Console.WriteLine(s);
Console.Read();
}
}
}