使用正则表达式中断字符串 [英] break a string using regular expressions

查看:83
本文介绍了使用正则表达式中断字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我正在开发一个Web应用程序,在该应用程序中,我从数据库中获取了一个字符串并将其断开以使用它.这是字符串:string str ="DB1031229";

但是有时候我没有得到正确的结果.我需要在:
中将其分解
DB

10

1229

您可能会注意到,我没有在其中包括3.我不需要那个.请提出使用正则表达式或某些无错误方式的解决方案.

提前谢谢.



谢谢,
Jagjot

Hello Friends,

I am working on a web application in which I am getting a string from database and breaking it to use it. This is the string: string str = "DB1031229";

But sometimes I am not getting proper results. I need to break it in :

DB

10

1229

You can notice that I have not included 3 in that. I dont need that. Please suggest a solution using Regular expressions or some error free way.

Thanks in advance.



Thanks,
Jagjot

推荐答案

如果字符串的长度始终相同,则可以使用字符串操作l
If the length of the string is always same you can use string operations l
string numbers = "DB1031229";
        string s0 = numbers.Substring(0, 2);
        string s9 = numbers.Substring(2, 2);
        string s10 = numbers.Substring(5);


using System.Text.RegularExpressions;
Regex r = new Regex("DB");
           var v = "DB1031229";
           string[] s = r.Split(v);
           Match n = r.Match(v);
             string v1=n.ToString();//DB
          string v2 = s[1].Substring(0, 2);//"10"
         string  v3 = s[1].Substring(3); //"1229"


希望(1) ^ ]和(2) MSDN [
Hope (1)Regular Expression[^]and (2)MSDN[^] will give you an idea.


这篇关于使用正则表达式中断字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆