Java String Conversion
In this chapter you will learn:
- How to Convert boolean, char, double, float,int,long,object to String
- How to Convert string case with locale
- 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 stringstatic String valueOf(char c)
converts char to stringstatic String valueOf(char[] data)
converts char array to stringstatic String valueOf(char[] data, int offset, int count)
converts sub char array to stringstatic String valueOf(double d)
converts double to stringstatic String valueOf(float f)
converts float to stringstatic String valueOf(int i)
converts int to stringstatic String valueOf(long l)
converts long to stringstatic 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 charsetbyte[] getBytes(Charset charset)
encodes String to byte array using given charsetbyte[] 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:
- Trim a string, remove the leading and ending spaces
- How to get sub string from a string
- How to check if a string is an empty string
- How to Get the length of a string
Home » Java Tutorial » String