有人可以向我解释一下什么会导致 split() 在索引 [0] 处索引一个空间吗?我发送的值是使用trim(),所以即使它确实有空格,它也会在分割之前被删除。然而,它的索引空间仍然为0。
public void firstName(){
System.out.printf("Enter your First Name:");
firstName = user_input.next().trim().toLowerCase();
}
public String getFirstName(){
return this.firstName;
}
public String splitFirstName(getInput input){
String x = input.getFirstName();
String[] splitValue = x.split("");
String myfirst = splitValue[0];
return myfirst;
}
最佳答案
您没有空格作为分隔符。您有一个空字符串。
String[] splitValue = x.split(""); //<-- this should be x.split(" ");
关于Java split() 在索引 0 处有空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23228275/