Regex 如何从split()方法获取拆分的字符串
嗨,朋友们,我正在尝试从用户在我的jsp表单中输入的全名中获取分开的名字和姓氏。在提交表单时收到全名后,我将全名拆分为两个字符串,并用空格分隔符分隔。这是解释我的问题的代码。请帮忙Regex 如何从split()方法获取拆分的字符串,regex,string,split,Regex,String,Split,嗨,朋友们,我正在尝试从用户在我的jsp表单中输入的全名中获取分开的名字和姓氏。在提交表单时收到全名后,我将全名拆分为两个字符串,并用空格分隔符分隔。这是解释我的问题的代码。请帮忙 String name = request.getParameter("name"); String userType = request.getParameter("usertype"); //split the name into first_
String name = request.getParameter("name");
String userType = request.getParameter("usertype");
//split the name into first_name and last_name
String splittedName[] = StringUtils.split(name);
System.out.println(Arrays.toString(splittedName));
String firstname = splittedName[0];
String surname = splittedName[1];
在调试应用程序时,在所有行上设置断点。尝试将splittedName[0]和splittedName[1]值转换为“firstname”和“姓氏”字符串时出错。请帮忙…对不起,我的问题中有任何愚蠢的错误
String name = "Kamlesh Sharma";
String splittedName[] = name.split(" ");
System.out.println(Arrays.toString(splittedName));
String firstName = splittedName[0];
String lastName = splittedName[1];
这将符合你的目的
注意:
您需要在split方法中提供分隔符,而不是要传递的名称发布错误报告。变量
name
的值是多少?使用StringUtils.split(name)
在下面的示例中起作用。使用调试器检查name的内容。