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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:02:52  来源:igfitidea点击:

Java - How do I find the length of a string in an array?

javaarraysstringalphabeticalstring-length

提问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 .lengthmethod on any string array element to get its length, And to sort that array alphabetically you ca use .sortmethod -

您可以简单地.length在任何字符串数组元素上使用方法来获取其长度,并按字母顺序对该数组进行排序,您可以使用.sort方法 -

For example -

例如 -

String[] strings = { "Hello ", "This ", "is", "Sorting", "Example" };
Arrays.sort(strings);

So after sorting strings[0]would be Exampleand string[0].length()would be 7

所以在排序之后strings[0]将会是Example并且string[0].length()将会是7