如果我有一个像这样的字符串:
This.is.a.great.place.too.work.
or:
This/is/a/great/place/too/work/
我的程序应该告诉我这个句子是有效的并且它有“作用”。
如果我有 :
This.is.a.great.place.too.work.hahahha
or:
This/is/a/great/place/too/work/hahahah
那么我的程序不应该告诉我句子中有“工作”。
所以我正在查看java字符串以在句子末尾找到一个单词.
or ,
or /
在它之前。我怎样才能实现这个目标?
这其实很简单,String http://docs.oracle.com/javase/8/docs/api/java/lang/String.html对象有一个endsWith http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#endsWith-java.lang.String- method.
从你的问题来看,你似乎想要/
, ,
or .
作为分隔符设置。
So:
String str = "This.is.a.great.place.to.work.";
if (str.endsWith(".work.") || str.endsWith("/work/") || str.endsWith(",work,"))
// ...
您还可以使用matches http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#matches%28java.lang.String%29方法和一个相当简单的正则表达式:
if (str.matches(".*([.,/])work\\1$"))
使用字符类[.,/]
指定句点、斜杠或逗号以及反向引用,\1
匹配找到的替代项(如果有)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)