Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have one string that is " my number is -----------------------------------------890"now i want sub string my number is that only

have any methods...

----------means spaces
Posted
Comments
Maciej Los 8-Aug-13 8:53am    
What have you done so far? What have you tried? Where are you stuck?
hebsiboy 8-Aug-13 9:08am    
I use one loop that is working fine.. but that is take long process so that i am asking any shortcut methods to split this string
joshrduncan2012 8-Aug-13 9:11am    
I suggest researching how String.Split() works.

Since you already know that you're searchning for "My number is", you don't need to know what it is.

But because you made the effort to write a question, I guess you're actually looking for whatever comes before the spaces. So you could try this:
C#
string input = "my number is -----------------------------------------890";

System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(" [ 0-9]");
System.Text.RegularExpressions.Match match = regex.Match(input);

string key = input.SubString(0, match.Index);
The regular expression checks for a space followed by another space or number. So for this to work, there
- must be at least one space between key and the phone number
- must not be any numbers starting words within key.

Or, what about this one:
C#
regex = new Regex("[0-9]{3,}");
Match match = regex.Match(input);
int numberIndex = match.Index;
string key = input.SubString(0, numberIndex);
key = key.Trim()


Ah, forget it all:
C#
string input = "my number is -----------------------------------------890";
string key = input.Trim(new Char[]{' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
 
Share this answer
 
Hi,

Use like the following:

C#
string original = "my phone number is                                       123456789";
            int pos = original.IndexOf("1");
            string strNew = original.Substring(0, pos).Trim();


In this way ou can able to get the required result.

Thanks
 
Share this answer
 
A lazy approach would be to trim all spaces and then but it back together;
C#
var i = "my phone number is                                       123456789";
var o = String.Join(" ", i.Split(' ', StringSplitOptions.RemoveEmptyEntries));


Hope this helps,
Fredrik
 
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