如何从大字符串中提取值? [英] How to extract values from large string?
本文介绍了如何从大字符串中提取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我遇到的情况是我必须提取大字符串中出现的所有值。例如:
Hi everyone,
I have come against a scenario where i have to extract all values where it occurs in a large string. For example:
string longString = "val{1}:val{5}/val{10}";
所以我想提取出现在 val {} 的所有值;
结果应该是:
1
5
10
我去过尝试使用indexOf做这个但运气不好。有什么方法可以做到这一点?
我很感激任何建议! :)
So i want to extract all values that occur where val{ };
The result should be:
1
5
10
I have been trying to do this using indexOf but with not much luck. What would be easy way to accomplish this?
I'd appreciate any advice! :)
推荐答案
string longString = "val{1}:val{5}/val{10}";
var Digits = Regex.Matches(longString, "[0-9]+");
List<string> LstDigit= new List<string>();
foreach (Match Digit in Digits)
{
LstDigit.Add(Digit.ToString());
}
问候..
Regards..
你好,
试试这个方式
Hello ,
Try this way
string longString = "val{124}:val{50}/val{100}";
//first split the string into two parts from semicolon
string[] sp=longString.Split(':');
//then get the first part by using substring , here 1234
Console.WriteLine("First Part is " + sp[0].ToString().Substring(4,sp[0].ToString().Length-5));
//again split the first array into two parts
string second=sp[1].ToString() ;
string[] secondarrary=second.Split('/');
//Now get second and third part using substring
Console.WriteLine("Second Part is " + secondarrary[0].ToString().Substring(4,secondarrary[0].Length-5));
Console.WriteLine("Third Part is " + secondarrary[1].ToString().Substring(4,secondarrary[1].Length-5));
和OP是
and OP is
First Part is 124
Second Part is 50
Third Part is 100
使用System.Text.RegularExpressions添加引用
add the reference
using System.Text.RegularExpressions;
然后添加以下代码
Then add the following code
string[] numbers = Regex.Split("val{1}:val{5}/val{10}", @"\D+").Where(x => !string.IsNullOrEmpty(x)).ToArray();
最后你会得到一个包含所有数字的字符串数组
And finally you will get a string array which will contain all the numbers
这篇关于如何从大字符串中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文