9

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

Jørgen
  • 8,820
  • 9
  • 47
  • 67
Rdhao
  • 107
  • 1
  • 1
  • 5

11 Answers11

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

may help you

6

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.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
4
String  array[]= {"some values"};

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

String array_uppercase[]=str.toLowerCase().split(',');
SuRa
  • 1,053
  • 9
  • 13
3

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
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
2

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.
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
1

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);
KarelG
  • 5,176
  • 4
  • 33
  • 49
  • It's not so advisable to use streams for that small set of static array. A simple loop is enough. (What you do is converting the array to list, then stream it, and then convert back to array. A ~3N operation while one simple loop reduces it to ~1N operation). – KarelG Mar 15 '17 at 11:31
0
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()]);
  }
} 
barfuin
  • 16,865
  • 10
  • 85
  • 132
0

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


    }

}
Rao
  • 20,781
  • 11
  • 57
  • 77
Robi
  • 1
0

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

mifthi
  • 151
  • 8
0

Two steps are needed:

  1. Iterate over the array of Strings
  2. Convert each one to lower case.
duffymo
  • 305,152
  • 44
  • 369
  • 561
0

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());

}
}
Noufal Panolan
  • 1,357
  • 2
  • 14
  • 27