java 将字符串存储在 char 数组中
在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您希望 char 数组在每个(或几乎每个索引)处保存字符串的每个字符,那么您可以这样做:
其中
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:
Where
length
is from 0 toname.length
.这段代码无法编译,因为 substring 方法只能在字符串上调用,而不能在字符串数组上调用(如果我没记错的话)。在上面的代码中,时间戳被声明为具有 40 个索引的字符串数组。
同样在这段代码中,您要求用户输入并将其分配给这一行中的名称:
然后您尝试将用户刚刚键入的内容替换为下一行时间戳中存储的内容,而下一行什么也没有,并且会删除名称中存储的任何内容:
而且无论如何这都不起作用,因为时间戳是一个由 40 个字符串组成的数组,而不是一个特定的字符串。为了调用子字符串,它必须只是一个特定的字符串。
我知道这可能对您想要做的事情没有太大帮助,但希望它可以帮助您理解为什么这不起作用。
如果您可以通过具体示例回复您想要执行的操作,我可以帮助您进一步指导。例如,假设您希望用户输入他们的名字“John Smith”,然后您希望将其分成两个不同的字符串变量或字符串数组中的名字和姓氏。你想做的事情越具体越好。祝你好运:)
开始编辑
好的,如果我理解你正在做什么,你可能想尝试以下一些事情。
如果您只想将名字和姓氏分开,您可以做的另一件事就是像这样拆分它。
该行在找到空格时将分割字符串并将其放入 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:
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:
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.
The other thing you could do if you wanted to just seperate the first and last name would be to split it like this.
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.
您在寻找
char[]
吗?您可以使用String.copyValueOf(char[])
将字符数组转换为字符串。Are you looking for a
char[]
? You can convert a character array to a String usingString.copyValueOf(char[])
.Java,对数组进行子串:
使用 Arrays.copyOfRange:
例如:
打印:
另一篇 stackoverflow 文章介绍了更多详细信息:
https://stackoverflow.com/a/6597591/445131
Java, substring an array:
Use Arrays.copyOfRange:
For example:
Prints:
Another stackoverflow post going into more details:
https://stackoverflow.com/a/6597591/445131