Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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);
 
Share this answer
 
Comments
Hiren solanki 19-Jan-11 3:44am    
Substring is good too.
justinonday 19-Jan-11 3:47am    
good
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"
 
Share this answer
 
Comments
Hiren solanki 19-Jan-11 3:44am    
Can you please make me know for following such a length work as of you can get it with simple substring ? as mkgoud represented.
justinonday 19-Jan-11 3:49am    
in the question use Regular Expression that's why .....
justinonday 19-Jan-11 3:50am    
anyway thanks Hiren
Hope (1)Regular Expression[^]and (2)MSDN[^] will give you an idea.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900