الثلاثاء، 30 يونيو 2020

Convert Any primitive to String Using ValueOf() in Java 8 - int to String

1. Introduction


In this article, You'll learn how to convert any primitive value to a String. Let us see the examples to convert int, long, double, char, boolean, float, and char[] array to String objects.

All of these primitive conversions are done using the String API valueOf() method. valueOf() method is an overloaded method in String class and declared a static method.

Java String ValueOf() - Primitive to String


Convert Any primitive to String Using ValueOf() in Java 8 - int to String


الخميس، 25 يونيو 2020

Java String toLowerCase​() Examples to convert string to lowercase

1. Introduction


In this article, You'll be learning how to convert each character in the string into a lower case.

This is very easy and can be done using String class toLowerCase() method which returns a new String after converting every character to the lowercase.

Java String toLowerCase​() Examples to convert string to lowercase


2. Java String toLowerCase​() Syntax


Below is the syntax from java api documentation.

public String toLowerCase​()
public String toLowerCase​(Locale locale)


This method does not take any argument but returns a new String with lowercase contents.

How to convert String to lowercase?

3. Java String toLowerCase​() Example - To convert String to Lowercase


package com.javaprogramto.w3schools.programs.string;

public class StringToLowercase {

    public static void main(String[] args) {

        String string = "HELLO WELCOME TO THE JAVAPROGRAMTO.COM";
        String lowercaseStr = string.toLowerCase();

        System.out.println("Lowercase converted string : "+lowercaseStr);
    }
}

Output:

Lowercase converted string : hello welcome to the javaprogramto.com

4. Java String toLowerCase​() Locale Examples


By default, toLowerCase() does conversion internally based on the default locale from the computer or server.

toLowerCase(Locale.getDefault());

I have set the default locale as English in my machine so conversion is done for this locale;

But, if you want to for different locale then use the toLowerCase(Locale locale) overloaded method instead of the toLowerCase() method.


package com.javaprogramto.w3schools.programs.string;

import java.util.Locale;

public class StringToLowercase {

    public static void main(String[] args) {

        Locale defaultLocale = Locale.getDefault();
        System.out.println("Default locale : "+defaultLocale);

        String string = "I am Now Using the different locales";

        Locale french = Locale.forLanguageTag("co-FR");

        System.out.println("french loale : "+french);
        String lowercaseStr = string.toLowerCase(french);

        System.out.println("Lowercase converted string in French : "+lowercaseStr);

        Locale chineesLocale = new Locale("zh", "CN");

        String chineeslowerCase = string.toLowerCase(chineesLocale);
        System.out.println("Chinees locale string : "+chineeslowerCase);

    }

}

Output:

Default locale : en_US

french loale : co_FR

Lowercase converted string in French : i am now using the different locales

Chinees locale string : i am now using the different locales

5. Java String toLowerCase​() Internal Implementation


public String toLowerCase() {
    return toLowerCase(Locale.getDefault());
}

public String toLowerCase(Locale locale) {
    return isLatin1() ? StringLatin1.toLowerCase(this, value, locale)
                      : StringUTF16.toLowerCase(this, value, locale);
}

6. Conclusion


In this article,  We've seen how to convert the string to lowercase with the default locale and with custom locale for specified country.

All examples showed are over GitHub.

الجمعة، 12 يونيو 2020

الأربعاء، 10 يونيو 2020

Java String regionMatches​() Method Example

1. Java String regionMatches​() Overview


In this tutorial, We'll learn about Java String API regionMatches​() method to compare two substrings. In other words, comparing regions of two strings.

This method is very handy when we want to compare portions of two strings. Instead of comparing all contents of Strings.

In previous article, We've discussed on String matches() method.

Java String API regionMatches​() Method Example


1.1 regionMatches() Syntax


public boolean regionMatches​(int toffset, String other, int ooffset, int len)
public boolean regionMatches​(boolean ignoreCase, int toffset, String other, int ooffset, int len)


Fist variant does case sensitive comparison
Second variant has option to ignore the case. If true, it ignores case when comparing.

Java String API regionMatches​()