Java - 如何在数组中找到字符串的长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20316926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - How do I find the length of a string in an array?
提问by Zach Fortney
All I need to know is how to find the length of the string inside of the array. I also would like to know how to find which string comes first in alphabetical order in the array.
我只需要知道如何在数组中找到字符串的长度。我还想知道如何在数组中按字母顺序查找哪个字符串排在第一位。
回答by Alex Vulaj
myArr[0].length()
myArr[0].length()
This will access the string at location 0, then get the length of it.
这将访问位置 0 处的字符串,然后获取它的长度。
Also, for sorting alphabetically, you can use
此外,要按字母顺序排序,您可以使用
Arrays.sort(myArr);
Arrays.sort(myArr);
回答by Mohammad Adil
You can simply use .length
method on any string array element to get its length,
And to sort that array alphabetically you ca use .sort
method -
您可以简单地.length
在任何字符串数组元素上使用方法来获取其长度,并按字母顺序对该数组进行排序,您可以使用.sort
方法 -
For example -
例如 -
String[] strings = { "Hello ", "This ", "is", "Sorting", "Example" };
Arrays.sort(strings);
So after sorting strings[0]
would be Example
and string[0].length()
would be 7
所以在排序之后strings[0]
将会是Example
并且string[0].length()
将会是7