今天工作,需要将一串信息,包括用户名、密码、邮箱的字符串,将用户名和邮箱截取出来。
原字符串为:yulv # 123456 # [email protected]
此处用Matcher和Pattern类会非常简单,这两个类是利用正则表达式匹配查找的相关类,详细可以参见java Pattern和Matcher详解
我的代码如下:
public class MatcherTest {
public static void main(String[] args) {
String str = "yulv # 123456 # [email protected]";
Matcher matcher=Pattern.compile("#").matcher(str);
if(matcher.find()){
System.out.println(matcher.start());
}else{
System.out.println("null");
}
//String receiverName=str.substring(0,matcher.start()).trim();
//System.out.println(receiverName);
}
}