cat_bug cat_bug - 1 year ago 80
Java Question

How to find a String of last 2 items in colon separated string

I have a string =

. On this input, I want to return the string
(third colon intact).

The string
should return
(there's always 4 items and 3 colons).

I could have a loop that counts colons and makes a string of characters after the second colon, but I was wondering if there exists some easier way to solve it.

Answer Source

You can use the split() method for your string.

String example = "ab:cd:ef:gh";
String[] parts = example.split(":");
System.out.println(parts[parts.length-2] + ":" + parts[parts.length-1]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download