[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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
0
Idarhillgaar
Asked:
Idarhillgaar
3 Solutions
 
StillUnAwareCommented:
use String.split("\\"), it will return a string array containing both parts of a string
0
 
StillUnAwareCommented:
oh sorry, wrong character, it sould be String.split("/")
0
 
StillUnAwareCommented:
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]);
  }
}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nadhuviCommented:
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
0
 
InteractiveMindCommented:
> if not try using stringTokenizer
An example of this, would be:

   String input = "Donald/172.0.9.9";

   StringTokenizer st1 = new StringTokenizer( input, "/" );

   String hostname = st1.nextToken();         // This will assign the data you want to these two Strings
   String ipAddress = st1.nextToken();         //

0
 
aozarovCommented:
Why not to use the InetAddress (I assume this is where you got the string) API [getHostName, getHostAddress]
see:

bsh % InetAddress a = InetAddress.getLocalHost();
bsh % a;
<ozarov/192.168.1.101>
bsh % a.getHostName();
<ozarov>
bsh % a.getHostAddress();
<192.168.1.101>


0
 
NaeemgCommented:
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);
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> String[] sa = s.split("/");

I would prefer the indexOf () approach because split () works only on >= 1.4
0
 
neonlinesProject managerCommented:
I think aozarov solution is best and we can implementas follows


try {
   java.net.InetAddress i = java.net.InetAddress.getLocalHost();
   System.out.println(i);                  // name and IP address
   System.out.println(i.getHostName());    // name
   System.out.println(i.getHostAddress()); // IP address only
   }
   catch(Exception e){e.printStackTrace();}
0
 
IdarhillgaarAuthor Commented:
Hi, I went with Tokeniser, however I would like to thank all for all the other methods.


Thanks!
Idar
0
 
InteractiveMindCommented:
Cheers, Idar. :-)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now