如何分割两个字符串 [英] how to split two strings
问题描述
我有字符串=("\" 20v2015-04-13 \")
我需要
的形式
字符串strvalue = 20;
字符串strDate = 2015-04-13;
Hi,
I have the string = ("\"20v2015-04-13\"")
i need it in the form of
string strvalue= 20;
string strDate=2015-04-13;
How is it possible?
推荐答案
使用正则表达式:
Use a regex:
(?<value>\d+)[a-zA-Z](?<date>\d{4}-\d\d-\d\d)</date></value>
应该做的-将数据提取为值"和日期"两类,您可以在没有剩余垃圾的情况下进行提取.
Should do it - it extracts the data into two groups "Value" and "Date" which you can pick up without the remaining rubbish.
//Use substring expects arg starting length
string str = ("\"20v2015-04-13\"")
//for your ans try this
string strDate = str.Substring(5, 10);
//OR
//see below link
http://csharp.net-informations.com/string/files/print/csharp-string-substring_print.htm [ ^ ]
http://csharp.net-informations.com/string/files/print/csharp-string-substring_print.htm[^]
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
作为解决方案1的补充,它为您提供了适合您需求的正则表达式,这是在C#中使用正则表达式的方式:
In complement to solution 1, which gives you an appropriate regular expression for your requirement, here is the way to use a regular expression in C#:
using System.Text.RegularExpressions;
string original = "20v2015-04-13";
Regex r = new Regex(@"(?<value>\d+)[a-zA-Z](?<date>\d{4}-\d\d-\d\d)", RegexOptions.Compiled | RegexOptions.CultureInvariant);
Match m = r.Match(original);
string value = String.Empty;
string date = String.Empty;
if (m.Success) {
value = m.Groups["value"].Value;
date = m.Groups["date"].Value;
}
有关.NET中正则表达式的更多信息,请访问:
System.Text.RegularExpressions命名空间 [ ^ ]
More on regular expressions in .NET here:
System.Text.RegularExpressions Namespace[^]
这篇关于如何分割两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!