2015-02-10 42 views
0

我有一个字符串变量,用一个空格分隔电子邮件地址和密码。例如:如何分解两个变量?

one_variable="[email protected] password" 

我想的电子邮件地址与密码分开,并从中创建两个字符串,像这样:

email_variable="[email protected]" 
password_variable="password" 

如何实现这一目标?

+7

通过简单地拆分'space' – vks 2015-02-10 06:17:58

+0

抱歉,但我不明白你..你能帮帮我吗? – Robot 2015-02-10 06:19:41

+0

[阅读本文](http://stackoverflow.com/q/3481828/3150943) – nu11p01n73R 2015-02-10 06:20:55

回答

2

尝试下面的代码:

String one_variable="[email protected] password"; 
String tok[]=one_variable.split(" "); 
System.out.println(tok[0]); 
System.out.println(tok[1]); 

代码基本上会溅到从空间的字符串。
如果你有多个空间,使用\\s+溢出即one_variable.split("\\s+")
输出:

[email protected] 
password 
+0

Please wait sir ..我会在检查完这个程序后回答你的答案。 – Robot 2015-02-10 06:21:53

+1

你也许想把它改成'\\ s +'来处理*多个空格*。 – TheLostMind 2015-02-10 06:22:46