By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

a way to ignore the rest of a string after a certain character??

P: 20
Hi guys, is there anything i can do to only read a certain part of a string, im reading an IP, but the IP contains the port like 127.0.0.1:80, but i dont want to include the ":80". i cant tell it how long the string is because the IP's change and the lengh varies. so i need to ignore anything after " : ".

Thanks
Mar 2 '09 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 750
PRR
you could use indexof and substring method (for short strings)...
Expand|Select|Wrap|Line Numbers
  1. string filterChar = ":";
  2.                 string myLongString = @"Some where rrr :  sdfsdf";
  3.                 int inde = myLongString.IndexOf(":");
  4.                 if (inde > 0)// or not equal to -1 will do 
  5.                 {
  6.                     string myShortString = myLongString.Substring(0, inde - 1);
  7.                 }
  8.  
A better approach would be regular expression for larger string ...
Mar 2 '09 #2

P: 20
Thanks deepblue, appreciate it!
Mar 2 '09 #3

100+
P: 283
only just learning regex's but this seems to work , I am getting the value from textbox1 to test it

Expand|Select|Wrap|Line Numbers
  1.  
  2. string RegexMatchString = @"(?<IP>\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})";
  3.             Regex testr = new Regex(RegexMatchString);
  4.             Match matx = testr.Match(textBox1.Text);
  5.             MessageBox.Show(matx.Groups["IP"].Value);
  6.  
  7.  
Mar 2 '09 #4

100+
P: 283
a bit simpler -

Expand|Select|Wrap|Line Numbers
  1.  
  2.             string RegexMatchString = @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}";
  3.             Regex testr = new Regex(RegexMatchString);
  4.             MessageBox.Show(testr.Match(textBox1.Text).Value) ;
  5.  
  6.  
  7.  
Mar 2 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.