【问题标题】:java store a string in a char arrayjava将字符串存储在char数组中
【发布时间】:2012-02-28 11:08:58
【问题描述】:

在 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);

【问题讨论】:

    标签: java arrays string substring


    【解决方案1】:

    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

    【讨论】:

      【解决方案2】:

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

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

      其中length 是从 0 到 name.length

      【讨论】:

        【解决方案3】:

        此代码无法编译,因为子字符串方法只能在字符串上调用,如果我没记错的话,不能在字符串数组上调用。在上面的代码中,timestamp 被声明为一个有 40 个索引的 String 数组。

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

        name = sc.nextLine();
        

        然后您尝试将用户刚刚键入的内容替换为下一行时间戳中存储的内容,这什么都没有,并且会删除名称中存储的内容:

        name = timestamp.substring(0,20);
        

        再一次,这无论如何也行不通,因为时间戳是一个由 40 个字符串组成的数组,而不是一个特定的字符串。为了调用子字符串,它必须是一个特定的字符串。

        我知道这可能对您尝试做的事情没有多大帮助,但希望它可以帮助您理解为什么这不起作用。

        如果您可以通过具体示例回复您正在尝试做的事情,我可以帮助您进一步指导。例如,假设您希望用户输入他们的姓名“John Smith”,然后您希望将其分成两个不同字符串变量或字符串数​​组中的名字和姓氏。越具体你想做的事就越好。祝你好运:)

        开始编辑

        好的,如果我理解您的操作正确,您可能想尝试以下几件事。

        //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。

        【讨论】:

        • 非常感谢。是的,我正在尝试接受用户输入,然后将名称的每个字符分配给字符串时间戳中的索引。仍然坚持下去。
        • @ayokunleadeosun 这可能有点令人困惑,但应该可以。如果您对其中任何部分的工作方式有疑问,请告诉我。我编辑了上面的答案,希望它能回答你的问题:)
        • @ayokunleadeosun 我认为您也可以只执行这一行,而不是 for 循环。 timestamp = name.toCharArray(); 这将与循环执行相同的操作。
        【解决方案4】:

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

        【讨论】:

        • 嗯。我可以将用户输入接收到 char 数组中吗?
        • 只需将其读取为String,然后使用String.toCharArray() 将其转换为可变的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-04
        • 2017-04-08
        • 2012-03-31
        相关资源
        最近更新 更多