繁体   English   中英

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

[英]java store a string in a char array

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

如果您想要一个char数组在每个(或几乎每个索引)处保存字符串的每个字符,则可以执行以下操作:

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

length从0到name.length

这段代码无法编译,因为substring方法只能在String上调用,如果我没有记错的话,则不能在String数组上调用。 在上面的代码中,时间戳记被声明为具有40个索引的String数组。

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

name = sc.nextLine();

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

name = timestamp.substring(0,20);

再说一次,无论如何都不会起作用,因为时间戳是一个由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,separateName [1] = Smith。

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM