0

Anyone knows how I can read a string value from a textbox (e.g.: 192.168.1.1), split the values using a delimiter, store the values and convert them into int for later use?

Thanks.

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by jlovesfishiee
0
string[] parts = textBox1.Text.Split('.');
foreach (string str in parts) {
    int myInt;
    if (Int32.TryParse(str, out myInt)) {
        // Do something with the value in myInt
    } else {
        // the string wasn't an integer, can do something if I want
    }
}
0

You should be able to do

string[] octets = ipTextBox.Text.Split('.');
int octet1 = Convert.ToInt32(octets[0]);
int octet2 = Convert.ToInt32(octets[1]);
//... etc.

But if you're specifically using an IP address, I'm sure there's a constructor for an IP address object of some sort that takes a string in dotted-decimal notation for an IP address IPAddress myIP = new IPAddress(ipTextBox.Text);

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.