简体   繁体   English

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

[英]java store a string in a char array

In Java, How can I store a string in an array? 在Java中,如何将字符串存储在数组中? For example: 例如:

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

Then how can I get the length of the string? 那我怎样才能得到字符串的长度呢?

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

If you want a char array to hold each character of the string at every (or almost every index), then you could do this: 如果您想要一个char数组在每个(或几乎每个索引)处保存字符串的每个字符,则可以执行以下操作:

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

Where length is from 0 to name.length . length从0到name.length

This code doesn't compile because the substring method can only be called on a String, not a String array if I'm not mistaken. 这段代码无法编译,因为substring方法只能在String上调用,如果我没有记错的话,则不能在String数组上调用。 In the code above, timestamp is declared as a String array with 40 indexes. 在上面的代码中,时间戳记被声明为具有40个索引的String数组。

Also in this code, you're asking for input from a user and assigning it to name in this line: 同样在此代码中,您要求用户输入并将其分配给此行中的名称:

name = sc.nextLine();

and then you are trying to replace what the user just typed with what is stored in timestamp on the next line which is nothing, and would erase whatever was stored in name: 然后您尝试用下一行中存储在时间戳中的内容替换用户刚刚键入的内容,这没有什么,并且会擦除名称中存储的内容:

name = timestamp.substring(0,20);

And again that wouldn't work anyway because timestamp is an array of 40 strings instead of one specific string. 再说一次,无论如何都不会起作用,因为时间戳是一个由40个字符串组成的数组,而不是一个特定的字符串。 In order to call substring it has to be just one specific string. 为了调用子字符串,它必须只是一个特定的字符串。

I know that probably doesn't help much with what you're trying to do, but hopefully it helps you understand why this isn't working. 我知道这可能对您要执行的操作并没有多大帮助,但是希望它可以帮助您了解为何此操作无效。

If you can reply with what you're trying to do with a specific example I can help direct you further. 如果您可以针对特定示例回答自己的问题,那么我可以帮助您进一步指导。 For example, let's say you wanted a user to type their name, "John Smith" and then you wanted to seperate that into a first and last name in two different String variables or a String array. 例如,假设您希望用户键入他们的名字“ John Smith”,然后想将其分成两个不同的String变量或String数组的名字和姓氏。 The more specific you can be with what you want to do the better. 您想做的事情越具体越好。 Good luck :) 祝好运 :)

BEGIN EDIT 开始编辑

Ok here are a few things you might want to try if I understand what you're doing correctly. 好吧,如果我了解您的操作正确,那么您可能需要尝试一些操作。

//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);
}

The other thing you could do if you wanted to just seperate the first and last name would be to split it like this. 如果只想分开名字和姓氏,您可以做的另一件事就是像这样将其分开。

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

That line will split the string when it finds a space and put it in the index in the seperateName array. 该行将在找到空格时将字符串拆分,并将其放入seperateName数组的索引中。 So if name was "John Smith", sperateName[0] = John and seperateName[1] = Smith. 因此,如果名称是“ John Smith”,则sperateName [0] = John,separateName [1] = Smith。

Java, substring an array: Java,为一个数组子字符串:

Use Arrays.copyOfRange: 使用Arrays.copyOfRange:

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

For example: 例如:

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

Prints: 印刷品:

rico
skipper
kowalski
---
skipper
kowalski

Another stackoverflow post going into more details: https://stackoverflow.com/a/6597591/445131 另一个stackoverflow帖子介绍了更多详细信息: https : //stackoverflow.com/a/6597591/445131

Are you looking for a char[] ? 您在寻找char[]吗? You can convert a character array to a String using String.copyValueOf(char[]) . 您可以使用String.copyValueOf(char[])将字符数组转换为String。

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

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