简体   繁体   English

如何从单个输入中获取两个字符串(单词之间用空格分隔)

[英]How to get two strings from a single input (words separated by a space)

I am trying to create two strings from a single input. 我试图从单个输入创建两个字符串。

So if the user inputs "John Doe" I want to have two strings J and D where J=John and D=Doe. 因此,如果用户输入“ John Doe”,我想拥有两个字符串J和D,其中J = John和D = Doe。

I tried using the scanner.nextLine() method but that won't let me create two strings from the single input. 我尝试使用Scanner.nextLine()方法,但这不能让我从单个输入创建两个字符串。

So essentially I want to create another string whenever the user inputs a space between words. 因此,基本上我想在用户输入单词之间的空格时创建另一个字符串。

If anyone can clear that up for me that would be wonderful! 如果有人能为我解决问题,那就太好了! I have just started studying computer science so this is all new to me. 我刚刚开始学习计算机科学,因此这对我来说是全新的。

To get two strings from a single input (words separated by a space) follow this step. 要从单个输入中获取两个字符串(单词之间用空格隔开),请执行以下步骤。

1) First of all split the string 1)首先拆分字符串
2) store with index 2)存储索引
3) print with single space 3)用单个空格打印

public class SpaceRemove {

    public static void main(String[] args) {
        String s = "The   java      is   best";
        String[] temp3 = s.split("\\s+"); // First step
        String a, b, c, d;
        a = temp3[0]; // Store "The" at index 0
        b = temp3[1]; // Store "java" at index 1
        c = temp3[2]; // Store "is" at index 2
        d = temp3[3]; // Store "best" at index 3
        for (String string : temp3) {
            System.out.println(" " + string.trim()); // Print the split string
        }
        System.out.println("You input: " + a + " " + b + " " + c + " " + d); // Print the result with equal space
                                                                                // between two string.
    }
}

What you want to do is split a string.Im assuming you are using java you can simply take your string and do: 你要做的是分割一个字符串。我假设你正在使用Java,你可以简单地把你的字符串做为:

String ex = "John Doe";
String[] parts = String.split(ex);
String part1 = array[0];
String part2 = array[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有两个状态 TG 接受来自 {a, b}* 的所有输入字符串,这些字符串不在 EVEN-EVEN 中? - Is there a two state TG that accepts all the input strings from {a, b}* that are not in EVEN-EVEN? 如何从 class 中的 strings.xml 获取字符串? - How can i get String from strings.xml in a class? 如何使两个字符串作为参数的函数 - How to make a function with two strings as arguments 如何使用正则表达式编写 SQL select 语句,该语句将以相反的顺序显示 LEFT 中的两个单词 - How do I write a SQL select statement using regular expressions that will display the two words in LEFT in reverse order 如何从其定义中得到二的补码的编码公式 - How can we get the encoding formula of two's completment from its definition 两个位串之间的XOR - XOR between two strings of bits 如何找到两个以上输入数字中的最大值 - How to find the greatest of more than two input numbers 对O(n)中的两个数组进行排序(时间和空间) - sort two arrays in O(n) (time and space) 两个寄存器的值相乘后如何得到乘积的值? - How to get the value of the product after multiplying the values of two registers? 构建一个仅接受单词 baa、ab 和 abb 且不接受其他更长或更短字符串的 FA - Build an FA that accepts only the words baa, ab, and abb and no other strings longer or shorter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM