java中使用字符串数组时,将其转换为小写
我在java中得到了一个一维字符串数组,其中我想将所有字符串更改为小写,然后将其与原始数组进行比较,这样我就可以让程序检查我的字符串/数组中是否没有大写字符。
我尝试过使用 x.toLowercase 但这只适用于单个字符串。 有没有办法将整个字符串转换为小写?
亲切的问候, 品牌
I got a one dimensional array of strings in java, in which i want to change all strings to lowercase, to afterwards compare it to the original array so i can have the program check whether there are no uppercase chars in my strings/array.
i've tried using x.toLowercase but that only works on single strings.
Is there a way for me to convert the whole string to lowercase?
Kind regards,
Brand
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
可能会帮助你
may help you
如果您想要一个简短的示例,您可以执行以下操作
If you want a short example, you could do the following
只有两行
输出:
Just two line
OUTPUT:
在 Java 中,没有简单的方法可以调用集合中每个元素的方法;您需要手动创建一个正确大小的新数组,遍历原始数组中的元素,然后按顺序将它们的小写类似物添加到新数组中。
但是,考虑到您具体想要执行的操作,您不需要立即比较整个数组来执行此操作,并且会产生复制所有内容的成本。您可以简单地遍历数组 - 如果您发现一个元素不等于其小写版本,则可以返回
false
。如果到达数组末尾而没有找到任何此类元素,则可以返回true
。事实上,这会更有效,因为:
There's no easy way to invoke a method on every element of a collection in Java; you'd need to manually create a new array of the correct size, walk through the elements in your original array and sequentially add their lowercased analogue to the new array.
However, given what you're specifically trying to do, you don't need to compare the whole array at once to do this, and incur the cost of copying everything. You can simply iterate through the array - if you find an element which is not equal to its lowercase version, you can return
false
. If you reach the end of the array without finding any such element, you can returntrue
.This would in fact be more efficient, since:
之前我们使用(在 Java < 8 中)
现在在 Java8 中:
Previously we used (In Java < 8)
Now in Java8 :
需要两个步骤:
Two steps are needed:
您可以将字符串数组转换为单个字符串,然后将其转换为小写,请按照下面的示例代码进行操作
you can convert the array of strings to single string and then convert it into lower case and please follow the sample code below
下面的代码可能对你有帮助
The following code may help you
您只需一行代码即可完成。只需复制并粘贴以下代码片段,根本不进行任何循环。
快乐的弦乐生活。 =D
You can do it with a single line of code. Just copy and paste the following snippet, without any looping at all.
Happy String Life. =D