java将字符串存储在char数组中

时间:2012-02-07 00:06:54

标签: java arrays string substring

在Java中,如何将字符串存储在数组中?例如:

//pseudocode:
name = ayo
string index [1] = a
string index [2] = y
string index [3] = o

然后我怎样才能得到字符串的长度?

// this code doesn't work
String[] timestamp = new String[40]; String name;
System.out.println("Pls enter a name and surname");
Scanner sc = new Scanner(System.in);
name = sc.nextLine();
name=timestamp.substring(0, 20);

4 个答案:

答案 0 :(得分:2)

如果你想让char数组在每个(或几乎每个索引)保存字符串的每个字符,那么你可以这样做:

char[] tmp = new char[length];
for (int i = 0; i < length; i++) {
    tmp[i] = name.charAt(i);
}

length从0到name.length的位置。

答案 1 :(得分:1)

此代码无法编译,因为只有在我没有弄错的情况下才能在String上调用substring方法,而不能调用String数组。在上面的代码中,timestamp被声明为一个包含40个索引的String数组。

同样在此代码中,您要求用户输入并将其分配到此行中的名称:

name = sc.nextLine();

然后你试图用下一行的时间戳替换用户输入的内容,这是什么都没有,并且会删除存储在名称中的内容:

name = timestamp.substring(0,20);

而且无论如何这都行不通,因为timestamp是一个包含40个字符串而不是一个特定字符串的数组。为了调用子字符串,它必须只是一个特定的字符串。

我知道这可能对你要做的事情没什么帮助,但希望它可以帮助你理解为什么这不起作用。

如果您可以使用特定示例回复您尝试做的事情,我可以帮助您进一步指导。例如,假设您希望用户键入其名称“John Smith”,然后您希望将其分为两个不同的String变量或String数组中的名字和姓氏。你可以用你想做的更好的方式做得更好。祝你好运:)

开始编辑

如果我理解你正确做了什么,这里有一些你可能想要尝试的事情。

//Since each index will only be holding one character, 
//it makes sense to use char array instead of a string array.
//This next line creates a char array with 40 empty indexes.
char[] timestamp = new char[40];

//The variable name to store user input as a string. 
String name;

//message to user to input name  
System.out.println("Pls enter a name and surname");

//Create a scanner to get input from keyboard, and store user input in the name variable  
Scanner sc = new Scanner(System.in);  
name = sc.nextLine();

//if you wanted the length of the char array to be the same
//as the characters in name, it would make more sense to declare it here like this
//instead of declaring it above.
char[] timestamp = new char[name.length()];


//For loop, loops through each character in the string and stores in
//indexes of timestamp char array. 
for(int i=0; i<name.length;i++)
{
    timestamp[i] = name.charAt(i);
}

如果你想分开名字和姓氏,你可以做的另一件事就是像这样分开它。

String[] seperateName = name.split(" ");

该行将在找到空格时将其拆分并将其放入seperateName数组中的索引中。因此,如果名称是“John Smith”,则sperateName [0] = John和seperateName [1] = Smith。

答案 2 :(得分:0)

您在寻找char[]吗?您可以使用String.copyValueOf(char[])将字符数组转换为String。

答案 3 :(得分:0)

Java,子串一个数组:

使用Arrays.copyOfRange:

public static <T> T[] copyOfRange(T[] original,
                                  int from,
                                  int to)

例如:

import java.util.*;
public class Main{
    public static void main(String args[]){
        String[] words = new String[3];
        words[0] = "rico";
        words[1] = "skipper";
        words[2] = "kowalski";

        for(String word : words){
            System.out.println(word);
        }   
        System.out.println("---");

        words = Arrays.copyOfRange(words, 1, words.length);
        for(String word : words){
            System.out.println(word);
        }   
    }   
}

打印:

rico
skipper
kowalski
---
skipper
kowalski

另一篇stackoverflow文章详细介绍: https://stackoverflow.com/a/6597591/445131

相关问题