java中使用字符串数组时,将其转换为小写

发布于 2024-12-19 18:18:47 字数 164 浏览 1 评论 0原文

我在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 技术交流群。

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

发布评论

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

评论(11

蘑菇王子 2024-12-26 18:18:47
arraylist.stream().map(s -> s.toLowerCase()).collect(Collectors.toList()) 

可能会帮助你

arraylist.stream().map(s -> s.toLowerCase()).collect(Collectors.toList()) 

may help you

地狱即天堂 2024-12-26 18:18:47

如果您想要一个简短的示例,您可以执行以下操作

String[] array = ...
String asString = Arrays.toString(array);
if(asString.equals(asString.toLowerCase())
   // no upper case characters.

If you want a short example, you could do the following

String[] array = ...
String asString = Arrays.toString(array);
if(asString.equals(asString.toLowerCase())
   // no upper case characters.
尹雨沫 2024-12-26 18:18:47
String  array[]= {"some values"};

String str= String.join(',',array);

String array_uppercase[]=str.toLowerCase().split(',');
String  array[]= {"some values"};

String str= String.join(',',array);

String array_uppercase[]=str.toLowerCase().split(',');
许久 2024-12-26 18:18:47

只有两行

     String[] array = {"One", "Two"};
    for(int i=0;i<array.length;i++){
        if(!array[i].equals(array[i].toLowerCase()))
            System.out.println("It contains uppercase char");
        array[i] = array[i].toLowerCase();
    }
    for(int i=0;i<array.length;i++)
        System.out.println(array[i]);

输出:

It contains uppercase char
It contains uppercase char
one
two

Just two line

     String[] array = {"One", "Two"};
    for(int i=0;i<array.length;i++){
        if(!array[i].equals(array[i].toLowerCase()))
            System.out.println("It contains uppercase char");
        array[i] = array[i].toLowerCase();
    }
    for(int i=0;i<array.length;i++)
        System.out.println(array[i]);

OUTPUT:

It contains uppercase char
It contains uppercase char
one
two
我不吻晚风 2024-12-26 18:18:47

在 Java 中,没有简单的方法可以调用集合中每个元素的方法;您需要手动创建一个正确大小的新数组,遍历原始数组中的元素,然后按顺序将它们的小写类似物添加到新数组中。

但是,考虑到您具体想要执行的操作,您不需要立即比较整个数组来执行此操作,并且会产生复制所有内容的成本。您可以简单地遍历数组 - 如果您发现一个元素不等于其小写版本,则可以返回false。如果到达数组末尾而没有找到任何此类元素,则可以返回 true

事实上,这会更有效,因为:

  • 如果您发现一个确实包含大写字符的元素,您就可以缩短进一步的评估。 (想象一下这样的情况,一百万个字符串数组的第一个元素是大写的;您刚刚节省了一百万次对 lowercase() 的调用)。
  • 您不必为在比较之外不会使用的整个额外数组分配内存。
  • 即使在最好的情况下,您提出的方案也将涉及通过数组进行一次迭代以获得小写版本,然后通过数组进行另一次迭代以实现等于。即使没有短路的可能性,在一次迭代中完成这两项工作也可能会更有效。

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 return true.

This would in fact be more efficient, since:

  • you get to short-circuit further evaluation if you find an element that does have uppercase characters. (Imagine the case where the first element of a million-string array has an uppercase; you've just saved on a million calls to lowercase()).
  • You don't have to allocate memory for the whole extra array that you won't be using beyond the comparison.
  • Even in the best case scenario, your proposed scenario would involve one iteration through the array to get the lowercase versions, then another iteration through the array to implement the equals. Doing both in a single iteration is likely to be more efficient even without the possibility of short-circuiting.
野生奥特曼 2024-12-26 18:18:47

之前我们使用(在 Java < 8 中)

String[] x = {"APPLe", "BaLL", "CaT"};
for (int i = 0; i < x.length; i++) {
    x[i] = x[i].toLowerCase();
}

现在在 Java8 中:

x= Arrays.asList(x).stream().map(String::toLowerCase).toArray(String[]::new);

Previously we used (In Java < 8)

String[] x = {"APPLe", "BaLL", "CaT"};
for (int i = 0; i < x.length; i++) {
    x[i] = x[i].toLowerCase();
}

Now in Java8 :

x= Arrays.asList(x).stream().map(String::toLowerCase).toArray(String[]::new);
呢古 2024-12-26 18:18:47

需要两个步骤:

  1. 迭代字符串数组
  2. 将每个字符串转换为小写。

Two steps are needed:

  1. Iterate over the array of Strings
  2. Convert each one to lower case.
国粹 2024-12-26 18:18:47

您可以将字符串数组转换为单个字符串,然后将其转换为小写,请按照下面的示例代码进行操作

public class Test {

public static void main(String[] args) {
    String s[]={"firsT ","seCond ","THird "};
    String str = " ";
      for (int i = 0; i < s.length; i++) {
      str = str + s[i];
      }

      System.out.println(str.toLowerCase());

}
}

you can convert the array of strings to single string and then convert it into lower case and please follow the sample code below

public class Test {

public static void main(String[] args) {
    String s[]={"firsT ","seCond ","THird "};
    String str = " ";
      for (int i = 0; i < s.length; i++) {
      str = str + s[i];
      }

      System.out.println(str.toLowerCase());

}
}
渡你暖光 2024-12-26 18:18:47
import java.util.*;

public class WhatEver {

  public static void main(String[] args) {
    List <String> list = new ArrayList();

    String[] x = {"APPLe", "BaLL", "CaT"};
    for (String a : x) {
      list.add(a.toLowerCase); 
    }
    x = list.toArray(new String[list.size()]);
  }
} 
import java.util.*;

public class WhatEver {

  public static void main(String[] args) {
    List <String> list = new ArrayList();

    String[] x = {"APPLe", "BaLL", "CaT"};
    for (String a : x) {
      list.add(a.toLowerCase); 
    }
    x = list.toArray(new String[list.size()]);
  }
} 
银河中√捞星星 2024-12-26 18:18:47

下面的代码可能对你有帮助

package stringtoupercasearray;

import java.util.Scanner;

/**
 *
 * @author ROBI
 */
public class StringToUperCaseArray {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int size;
        String result = null;
        System.out.println("Please enter the size of the array: ");
        Scanner r=new Scanner(System.in);
        size=r.nextInt();
        String[] s=new String[size];
        System.out.println("Please enter the sritngs:");
        for(int i=0;i<s.length;i++){

        s[i]=r.next();

        }
        System.out.print("The given sritngs are:");
        for (String item : s) {
            //s[i]=r.nextLine();
            System.out.print(item+"\n");
        }

         System.out.println("After converting to uppercase the string is:");
        for (String item : s) {
            result = item.toUpperCase();
             System.out.println(result);
        }


    }

}

The following code may help you

package stringtoupercasearray;

import java.util.Scanner;

/**
 *
 * @author ROBI
 */
public class StringToUperCaseArray {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int size;
        String result = null;
        System.out.println("Please enter the size of the array: ");
        Scanner r=new Scanner(System.in);
        size=r.nextInt();
        String[] s=new String[size];
        System.out.println("Please enter the sritngs:");
        for(int i=0;i<s.length;i++){

        s[i]=r.next();

        }
        System.out.print("The given sritngs are:");
        for (String item : s) {
            //s[i]=r.nextLine();
            System.out.print(item+"\n");
        }

         System.out.println("After converting to uppercase the string is:");
        for (String item : s) {
            result = item.toUpperCase();
             System.out.println(result);
        }


    }

}
永不分离 2024-12-26 18:18:47

您只需一行代码即可完成。只需复制并粘贴以下代码片段,根本不进行任何循环。

String[] strArray = {"item1 Iteme1.1", "item2", "item3", "item4", "etc"}//This line is not part of the single line (=D).

String strArrayLowerCase[] = Arrays.toString(strArray).substring(1)
            .replace("]", "").toLowerCase().split(",");

快乐的弦乐生活。 =D

You can do it with a single line of code. Just copy and paste the following snippet, without any looping at all.

String[] strArray = {"item1 Iteme1.1", "item2", "item3", "item4", "etc"}//This line is not part of the single line (=D).

String strArrayLowerCase[] = Arrays.toString(strArray).substring(1)
            .replace("]", "").toLowerCase().split(",");

Happy String Life. =D

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