[英]How to extract part of a domain from a string in Java?
If my domain is 31.example.appspot.com
(App Engine prepends a version number), I can retrieve the domain from the Request object like this: 如果我的域是
31.example.appspot.com
(App Engine 31.example.appspot.com
了版本号),则可以从Request对象中检索域,如下所示:
String domain = request.getServerName();
// domain == 31.example.appspot.com
What I want is to extract everything except the version number so I end up with two values: 我想要的是提取除版本号以外的所有内容,因此最终得到两个值:
String fullDomain; // example.appspot.com
String appName; // example
Since the domain could be anything from: 由于域可以是以下任何来源:
1.example.appspot.com
to: 至:
31.example.appspot.com
How do I extract the fullDomain
and appName
values in Java? 如何在Java中提取
fullDomain
和appName
值?
Would a regex be appropriate here? 正则表达式在这里合适吗?
If you are always sure of this pattern, then just find the first dot and start from there. 如果您始终确信这种模式,则只需找到第一个点并从此处开始即可。
fullDomain = domain.subString(domain.indexOf('.'));
UPDATE : after James and Sean comments, here is the full correct code: 更新 :在James和Sean评论之后,这是完整的正确代码:
int dotIndex = domain.indexOf(".")+1;
fullDomain = domain.substring(dotIndex);
appName = domain.substring(dotIndex,domain.indexOf(".",dotIndex));
看一下java.lang.String上的split方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.