Reputation: 923
I have a requirement that i have to get a specific portion from a String i.e if i have java.lang.String i need to get the com field from the String i have done this
public class uu {
public static void main(String[] args) {
String domain = "www.xyzw.com";
String[] strArray = domain.split("\\.");
for (String str : strArray) {
System.out.println(str);
}
}
}
it is giving me three fields separately but i need the com only .. someone help me
Upvotes: 0
Views: 67
Reputation: 2874
If you work with domains you could use Google Guava InternetDoMainNames
class. It is very simple
InternetDomainName.from("www.xyzw.com").publicSuffix();
How to use: https://code.google.com/p/guava-libraries/wiki/InternetDomainNameExplained http://koziolekweb.pl/2014/03/30/pomocna-guava-informacje-o-domenie/ (with example - translation to english is posible)
Upvotes: 0
Reputation: 53809
You can do:
String r = strArray[strArray.length - 1];
Or:
String r = domain.substring(domain.lastIndexOf(".") + 1);
Upvotes: 1
Reputation: 54672
String domain = "www.xyzw.com";
String[] strArray = domain.split("\\.");
System.out.println(strArray[2]);
Upvotes: 1
Reputation: 54611
String s = domain.substring(domain.lastIndexOf(".")+1);
Consider inserting error checks for the case that the string does not contain .
Upvotes: 1