我目前正在做一个android项目。我想知道字符串是否包含'\r'或'\n'作为最后一个字符。如何进行图案匹配?
试试这个。
String string = "stackoverflow";
lastchar = string.substring(string.length() - 1);
它会给你一个"w"的结果。
尝试
String patterntomatch ="^[_A-Za-z0-9-]*(rn)$";
Pattern pattern=Pattern.compile(patterntomatch);
Matcher matcher=pattern.matcher(matchfromedittext);
boolean matcher.matches();
String last = your_string.substring(Math.max(your_string.length() - 2, 0));
//It will give you the last 2 characters of the string. If the string is null
//or has less than 2 characters, then it gives you the original string.
if(last.equals("r") || last.equals("n")){
//String's last two characters are either n or r. Now do something.
}