[英]How to split string with date in c#
i have string with date , i want to split it with date and string 我有日期字符串,我想用日期和字符串拆分它
For example : I have this type of strings data 例如:我有这种类型的字符串数据
9/23/2013/marking abandoned based on notes below/DB
12/8/2012/I think the thid is string/SG
and i want to make it like as 我想让它像
9/23/2013 marking abandoned based on notes below/DB
12/8/2013 I think the thid is string/SG
so, i don't know how to split these strings and store in different columns of table. 所以,我不知道如何拆分这些字符串并存储在表的不同列中。 pls help me.
请帮助我。
string[] vals = { "9/23/2013/marking abandoned based on notes below/DB",
"12/8/2012/I think the thid is string/SG" };
var regex = @"(\d{1,2}/\d{1,2}/\d{4})/(.*)";
var matches = vals.Select(val => Regex.Match(vals, regex));
foreach (var match in matches)
{
Console.WriteLine ("{0} {1}", match.Groups[1], match.Groups[2]);
}
prints: 打印:
9/23/2013 marking abandoned based on notes below/DB
12/8/2012 I think the thid is string/SG
(\\d{1,2}/\\d{1,2}/\\d{4})/(.*)
breaks down to (\\d{1,2}/\\d{1,2}/\\d{4})/(.*)
分解为
(\\d{1,2}/\\d{1,2}/\\d{4})
: (\\d{1,2}/\\d{1,2}/\\d{4})
:
\\d{1,2}
- matches any one or two digit number \\d{1,2}
- 匹配任何一位或两位数字 /
- matches to one /
symbol /
- 匹配一个/
符号 \\d{4}
- matches to four digit number \\d{4}
- 匹配四位数字 (...)
- denotes first group (...)
- 表示第一组 (.*)
- matches everything else and creates second group (.*)
- 匹配其他所有内容并创建第二组 Perhaps with pure string methods, the third slash separates the date and the text: 也许使用纯字符串方法,第三个斜杠分隔日期和文本:
string line = "9/23/2013/marking abandoned based on notes below/DB";
int slashIndex = line.IndexOf('/');
if(slashIndex >= 0)
{
int slashCount = 1;
while(slashCount < 3 && slashIndex >= 0)
{
slashIndex = line.IndexOf('/', slashIndex + 1);
if(slashIndex >= 0) slashCount++;
}
if(slashCount == 3)
{
Console.WriteLine("Date:{0} Text: {1}"
, line.Substring(0, slashIndex)
, line.Substring(slashIndex +1));
}
}
For what it's worth, here is a extension method to "break" a string in half on nth occurence of astring: 对于它的价值,这里是一个扩展方法,在第n次出现astring时“断”一半的字符串:
public static class StringExtensions
{
public static string[] BreakOnNthIndexOf(this string input, string value, int breakOn, StringComparison comparison)
{
if (breakOn <= 0)
throw new ArgumentException("breakOn must be greater than 0", "breakOn");
if (value == null) value = " "; // fallback on white-space
int slashIndex = input.IndexOf(value, comparison);
if (slashIndex >= 0)
{
int slashCount = 1;
while (slashCount < breakOn && slashIndex >= 0)
{
slashIndex = input.IndexOf(value, slashIndex + value.Length, comparison);
if (slashIndex >= 0) slashCount++;
}
if (slashCount == breakOn)
{
return new[] {
input.Substring(0, slashIndex),
input.Substring(slashIndex + value.Length)
};
}
}
return new[]{ input };
}
}
Use it in this way: 以这种方式使用它:
string line1 = "9/23/2013/marking abandoned based on notes below/DB";
string line2 = "12/8/2012/I think the thid is string/SG";
string[] res1 = line1.BreakOnNthIndexOf("/", 3, StringComparison.OrdinalIgnoreCase);
string[] res2 = line2.BreakOnNthIndexOf("/", 3, StringComparison.OrdinalIgnoreCase);
Another way to do it with LINQ: 使用LINQ的另一种方法:
var inputs = new[]{
"9/23/2013/marking abandoned based on notes below/DB",
"12/8/2012/I think the thid is string/SG"
};
foreach (var item in inputs)
{
int counter = 0;
var r = item.Split('/')
.Aggregate("", (a, b) =>
a + ((counter++ == 3) ? "\t" : ((counter == 1) ? "" : "/")) + b);
Console.WriteLine(r);
}
Or you may use the IndexOf
and Substring
methods: 或者您可以使用
IndexOf
和Substring
方法:
foreach (var item in inputs)
{
var lastPos =
item.IndexOf('/',
1 + item.IndexOf('/',
1 + item.IndexOf('/')));
if (lastPos != -1)
{
var r = String.Join("\t",
item.Substring(0, lastPos),
item.Substring(lastPos + 1, item.Length - lastPos - 1));
Console.WriteLine(r);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.