Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

manipulating a string 2

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
0
0
TH
Hello,

I have a string "<sip:username@10.10.10.10:5060>"
I would like just to get the "username", and nothing else.

Code:
string callStatus = "<sip:username@10.10.10.10:5060>";
string usernameAndIP = callStatus.Remove(0, 5);
string[] username = usernameAndIP.Split('@');
this.lblCallStatus.Text = username[0].ToString();

I am using this code above. But and just Interested if there is a better more efficient way to do this.
Just looking to improve my code if that is possible, and also will this work for every situation that could arise. For example if the port number hasn't been included.

Many thanks,
 
Have a look at Regular Expressions - they are perfect for that sort of task.


Hope this helps.

[vampire][bat]
 
Two lines and easy to read:

Code:
			string callStatus = "<sip:username@10.10.10.10:5060>";
			lblCallStatus.Text = Regex.Match(callStatus, "(?<=\\<sip:)[a-z]+(?=@)").ToString();

Translates to:
return any number of characters that are preceeded by "[tt]<sip:[/tt]" and followed by "[tt]@[/tt]"


[wink]

[vampire][bat]
 
lines and even eassier to read

Code:
string callStatus = "<sip:username@10.10.10.10:5060>";
lblCallStatus.Text = callStatus.Remove(0, 5).Split('@')[0].ToString();

And shorter. ;-)

Christiaan Baes
Belgium

The future is out there
my blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top