如何分割两个字符串

编程入门 行业动态 更新时间:2024-10-27 04:27:44
本文介绍了如何分割两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有字符串=("\" 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

csharp-informations/string/files/print/csharp-string-substring_print.htm [ ^ ]

csharp-informations/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[^]

更多推荐

如何分割两个字符串

本文发布于:2023-11-29 01:40:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!