Link to home
Start Free TrialLog in
Avatar of Idarhillgaar
IdarhillgaarFlag for Ireland

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
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

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]);
  }
}
Avatar of nadhuvi
nadhuvi

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
ASKER CERTIFIED SOLUTION
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
Avatar of Mayank S
>> String[] sa = s.split("/");

I would prefer the indexOf () approach because split () works only on >= 1.4
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Idarhillgaar

ASKER

Hi, I went with Tokeniser, however I would like to thank all for all the other methods.


Thanks!
Idar
Cheers, Idar. :-)