Java String Conversion

In this chapter you will learn:

  1. How to Convert boolean, char, double, float,int,long,object to String
  2. How to Convert string case with locale
  3. How to encode a string into byte array

Convert boolean, char, double, float,int,long,object to String

The following static methods from String class converts primitive types to String.

  • static String valueOf(boolean b) converts boolean to string
  • static String valueOf(char c) converts char to string
  • static String valueOf(char[] data) converts char array to string
  • static String valueOf(char[] data, int offset, int count) converts sub char array to string
  • static String valueOf(double d) converts double to string
  • static String valueOf(float f) converts float to string
  • static String valueOf(int i) converts int to string
  • static String valueOf(long l) converts long to string
  • static String valueOf(Object obj) converts Object to string
public class Main {
  public static void main(String[] argv) {
    String str = String.valueOf(123);/* jav  a 2  s  .c  om*/

    System.out.println(str);
  }
}

The output:

public class Main {
  public static void main(String args[]) {
    char charArray[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    boolean booleanValue = true;
    char characterValue = 'Z';
    int integerValue = 7;
    long longValue = 10000000000L; // L suffix indicates long
    float floatValue = 2.5f; // f indicates that 2.5 is a float
    double doubleValue = 3.3; // no suffix, double is default
    Object objectRef = "hello"; // assign string to an Object reference
/*from  jav a  2  s  .  c  o m*/
    System.out.printf("char array = %s\n", String.valueOf(charArray));
    System.out.printf("part of char array = %s\n", String.valueOf(charArray, 
                              3, 3));
    System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
    System.out.printf("char = %s\n", String.valueOf(characterValue));
    System.out.printf("int = %s\n", String.valueOf(integerValue));
    System.out.printf("long = %s\n", String.valueOf(longValue));
    System.out.printf("float = %s\n", String.valueOf(floatValue));
    System.out.printf("double = %s\n", String.valueOf(doubleValue));
    System.out.printf("Object = %s\n", String.valueOf(objectRef));
  }
}

The output:

Convert string case with locale

We can use the following methods to change the case of a string. We can also choose a Locale during the changing. A locale is a settings for a specific languages or countries.

  • String toLowerCase() converts String to lower case using default locale.
  • String toLowerCase(Locale locale) converts String to lower case using given Locale.
  • String toUpperCase() converts String to upper case using default locale.
  • String toUpperCase(Locale locale) converts String to upper case using given Locale.

The following code defines a string first and then change its case to upper and lower.

public class Main {
  public static void main(String[] argv) {
    String str = "Demo2s.com";
    System.out.println(str.toLowerCase());
    System.out.println(str.toUpperCase());
//from j  a  v  a 2  s .com
  }
}

The output:

Convert string to byte array

Sometime we need to convert string value to byte array before we can transfer them through the network or save them to file or database. During the converting we can encode the string in specified charset.

  • byte[] getBytes() encodes String to byte array using default charset
  • byte[] getBytes(Charset charset) encodes String to byte array using given charset
  • byte[] getBytes(String charsetName) encodes String into byte array using the named charset
import java.util.Arrays;
/*  ja va 2 s  .  c  o  m*/
public class Main {
  public static void main(String[] argv) {
    String str = "     d e m o 2 s.com    ";
    byte[] bytes = str.getBytes();
    System.out.println(Arrays.toString(bytes));
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Trim a string, remove the leading and ending spaces
  2. How to get sub string from a string
  3. How to check if a string is an empty string
  4. How to Get the length of a string
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words