The String class provides methods to change a string to uppercase or lowercase. That is, you can change all characters in a string to uppercase, or all characters to lowercase.
toUpperCase()
toUpperCase()
changes the string to all uppercase. upper case means upper case.
String str = "Hello World";
System.out.println(str.toUpperCase());
str = "hello world";
System.out.println(str.toUpperCase());
Output:
HELLO WORLD
HELLO WORLD
toLowerCase()
toLowerCase()
changes the string to all uppercase. lower case means lower case.
String str = "Hello World";
System.out.println(str.toLowerCase());
str = "HELLO WORLD";
System.out.println(str.toLowerCase());
Output:
hello world
hello world
Change the case of different languages
In the example above, toUpperCase()
changed the English string to all uppercase letters.
In fact, this method does not change for English, but for the language returned by Locale.getDefault()
.
By default Locale.getDefault().getLanguage()
returns english, so it seemed to convert to uppercase for english.
// String.java
public String toLowerCase() {
return toLowerCase(Locale.getDefault());
}
However, if you pass Locale as an argument as follows, you can implement to change strings in other languages to all uppercase letters.
// String.java
public String toUpperCase(Locale locale)
Taking Turkish as an example, you can change it to uppercase like this:
Locale TURKISH = new Locale("tr");
System.out.println("\u0069".toUpperCase());
System.out.println("\u0069".toUpperCase(TURKISH));
Output:
I
İ
In the case of toLowerCase()
, you can pass Locale as an argument in the same way. Apply as above.
public String toLowerCase(Locale locale)
Compare case-insensitively
equals()
performs case-sensitive comparisons. On the other hand, equalsIgnoreCase()
does a case-insensitive string comparison.
String str = "Hello";
System.out.println("equals ? " + "hello".equals(str));
System.out.println("equalsIgnoreCase ? " + "hello".equalsIgnoreCase(str));
Output:
equals ? false
equalsIgnoreCase ? true
So, to make a case-insensitive comparison, you can use equalsIgnoreCase()
.
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)