我目前正在开发一个Android项目。我想知道一个字符串是否包含'\ r'或'\ n'作为最后一个字符。如何进行模式匹配?
答案 0 :(得分:0)
试试这个。
String string = "stackoverflow";
lastchar = string.substring(string.length() - 1);
它会给你结果" w"。
答案 1 :(得分:0)
试
String patterntomatch ="^[_A-Za-z0-9-]*(\r\n)$";
Pattern pattern=Pattern.compile(patterntomatch);
Matcher matcher=pattern.matcher(matchfromedittext);
boolean matcher.matches();
答案 2 :(得分:0)
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.
}