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

发布于 2025-01-02 21:02:22 字数 415 浏览 2 评论 0原文

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

In Java, How can I store a string in an array? 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云巢 2025-01-09 21:02:22

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

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

其中 length 是从 0 到 name.length

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[] tmp = new char[length];
for (int i = 0; i < length; i++) {
    tmp[i] = name.charAt(i);
}

Where length is from 0 to name.length.

北风几吹夏 2025-01-09 21:02:22

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

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

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,seerateName[1] = Smith。

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. In the code above, timestamp is declared as a String array with 40 indexes.

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. 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. 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. So if name was "John Smith", sperateName[0] = John and seperateName[1] = Smith.

離殇 2025-01-09 21:02:22

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

Are you looking for a char[]? You can convert a character array to a String using String.copyValueOf(char[]).

高跟鞋的旋律 2025-01-09 21:02:22

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

Java, substring an array:

Use 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文