When using an array of strings in java, convert it to lowercase

I have a one-dimensional array of strings in java in which I want to change all strings to lower case so that I can compare it with the original array so that I can check the program for capital letters in my strings / array.

I tried using x.toLowercase, but only works with single lines. Is there a way to convert the entire string to lowercase?

Regards, Brand

+6
source share
11 answers

If you need a short example, you can do the following

String[] array = ... String asString = Arrays.toString(array); if(asString.equals(asString.toLowerCase()) // no upper case characters. 
+5
source
 arraylist.stream().map(s -> s.toLowerCase()).collect(Collectors.toList()) 

can help you

+4
source

There is no easy way to call a method for each item in a collection in Java; you need to manually create a new array of the correct size, go through the elements in the original array and sequentially add their lower counterpart to the new array.

However, given what you are specifically trying to do, you do not need to compare the entire array at once to do this, and bear the cost of copying everything. You can simply iterate over the array - if you find an element that is not equal to its string version, you can return false . If you reach the end of the array without finding such an element, you can return true .

This would be actually more effective because:

  • You can further evaluate the short circuit if you find an element that has uppercase characters. (Imagine the case where the first element of an array in a millionth row is uppercase, you just saved a million calls in lowercase ()).
  • You do not need to allocate memory for the entire additional array, which you will not use outside of the comparison.
  • Even in the best case, your proposed scenario will include one iteration through an array to get lowercase versions, and then another iteration through an array to implement equal. Executing as in one iteration is likely to be more efficient even without the possibility of a short circuit.
+2
source

Just two lines

  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 
+2
source
 String array[]= {"some values"}; String str= String.join(',',array); String array_uppercase[]=str.toLowerCase().split(','); 
+2
source

Two steps are required:

  • Iterate over an array of strings
  • Convert each one to lowercase.
+1
source

Earlier 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); 
+1
source

you can convert an array of strings to a single string and then convert it to lowercase and please follow the code example 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()); } } 
0
source
 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()]); } } 
0
source

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); } } } 
0
source

You can do this with a single line of code. Just copy and paste the following snippet without any loops.

 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

0
source

Source: https://habr.com/ru/post/1385111/


All Articles