Idarhillgaar
asked on
How to split up a String
From Inetaddress I can retrieve HostName and HostIpaddress (Donald/172.18.5.192)
What I want to do is to divide this String into String1 = Donald and String2 = 172.18.5.192.
The First String is the letters until "/" and after "/" is then the IP address.
Tried searching for this solution, but it may not be so common.
Please help if you can,
Thanks
What I want to do is to divide this String into String1 = Donald and String2 = 172.18.5.192.
The First String is the letters until "/" and after "/" is then the IP address.
Tried searching for this solution, but it may not be so common.
Please help if you can,
Thanks
use String.split("\\"), it will return a string array containing both parts of a string
oh sorry, wrong character, it sould be String.split("/")
Here it goes:
public class strSplit {
public static void main(String[] args) {
String s = "Donald/172.18.5.192";
String[] sa = s.split("/");
for(int i = 0; i < sa.length; i++)
System.out.println(sa[i]);
}
}
public class strSplit {
public static void main(String[] args) {
String s = "Donald/172.18.5.192";
String[] sa = s.split("/");
for(int i = 0; i < sa.length; i++)
System.out.println(sa[i]);
}
}
If u know it s containg onli this then use the following
String test = "Donald/172.0.9.9"; // yr input string
int index = test.indexOf("/");
String name = test.subString(0,index);
String ipaddess = test.subString(index);
i think this will help u..
if not try using stringTokenizer
String test = "Donald/172.0.9.9"; // yr input string
int index = test.indexOf("/");
String name = test.subString(0,index);
String ipaddess = test.subString(index);
i think this will help u..
if not try using stringTokenizer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try this
String str = "Donald/172.18.5.192";
String str1 = str.substring(0, str.indexOf("/"));
String str2 = str.substring(str.indexOf( "/") + 1 , str.length());
System.out.println("str1 = " + str1 +
"\nstr2 = " + str2);
String str = "Donald/172.18.5.192";
String str1 = str.substring(0, str.indexOf("/"));
String str2 = str.substring(str.indexOf(
System.out.println("str1 = " + str1 +
"\nstr2 = " + str2);
>> String[] sa = s.split("/");
I would prefer the indexOf () approach because split () works only on >= 1.4
I would prefer the indexOf () approach because split () works only on >= 1.4
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi, I went with Tokeniser, however I would like to thank all for all the other methods.
Thanks!
Idar
Thanks!
Idar
Cheers, Idar. :-)