Java String Methods
Java String Methods
Java String Methods
Output: true
true
false
Java String format()
The java string format() method returns the formatted string by given locale, format and
arguments.
If you don't specify the locale in String.format() method, it uses default locale by
calling Locale.getDefault() method.
The format() method of java language is like sprintf() function in c language and printf() method of
java language.
Java String format() method example
class Format1{
public static void main(String args[]){
String name="Welcome";
String sf1=String.format("name is %s",name);
String sf2=String.format("value is %f",32.33434);
String sf3=String.format("value is %32.12f",32.33434);
System.out.println(sf1);
System.out.println(sf2);
System.out.println(sf3);
}}
Output:-
name is Welcome
value is 32.334340
value is 32.334340000000
Java String getBytes()
The java string getBytes() method returns the byte array of the string. In
other words, it returns sequence of bytes.
Java String getBytes() method example
class StringGetBytes1{
public static void main(String args[])
{
String s1="ABCDEFG";
byte[] aa=s1.getBytes();
for(int i=1;i<aa.length;i++)
{
System.out.println(aa[i]);
}
} Output: 66
} 67
68
68
70
71
Java String getChars()
The java string getChars() method copies the content of this string into specified char array. There
are 4 arguments passed in getChars() method. The signature of getChars() method is given below:
Java String getChars() method example
class StringGetChar1{
public static void main(String args[]){
String s1=new String("Hello Everybody How r u");
char[] ch=new char[10];
try{
s1.getChars(6,16,ch,0);
System.out.println(ch);
}
catch(Exception ex){
System.out.println(ex);
}
}}
Output: Everybody
Java String indexOf()
The java string indexOf() method returns index of given character value or
substring. If it is not found, it returns -1. The index counter starts from zero.
Java String indexOf() method example
class IndexOf1{
public static void main(String args[]){
String s1=new String("Hello Everybody How r u");
int index1=s1.indexOf("Everybody");
int index2=s1.indexOf("r");
System.out.println(index1+" "+index2);
int index3=s1.indexOf("Everybody",4);
System.out.println(index3);
int index4=s1.indexOf('d');
System.out.println(index4);
}}
Java String intern()
The java string intern() method returns the interned string. It returns
the canonical representation of string.
It can be used to return string from memory, if it is created by new
keyword. It creates exact copy of heap string object in string constant
pool.
Java String intern () method example
class Intern1{
public static void main(String args[]){
String s1=new String("Hello");
String s2="Hello";
String s3=s1.intern();
System.out.println(s1==s2);
System.out.println(s2==s3);
}}
Output: false
true
Java String isEmpty()
The java string isEmpty() method checks if this string is
empty or not. It returns true, if length of string is 0
otherwise false. In other words, true is returned if string is
empty otherwise it returns false.
Java String isEmpty() method example
class IsEmpty1{
public static void main(String args[]){
String s1="";
String s2="Amandeep";
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
}}
Java String join()
The java string join() method returns a string joined with given
delimiter. In string join method, delimiter is copied for each elements.
In case of null element, "null" is added. The join() method is included in
java string since JDK 1.8.
Java String join() method example
class StringJoin1
{
public static void main(String args[])
{
String joinString1=String.join("-
","Welcome","to","Jalandhar");
System.out.println(joinString1);
}
}
Java String lastIndexOf()
The java string split() method splits this string against given regular expression
and returns a char array.
Java String split() method example
class Split1
{
public static void main(String args[])
{
String s1="my name is amandeep";
String[] words=s1.split("\\s");
for(String w:words)
{
System.out.println(w);
}
} Output: my
} name
is
amandeep
Java String
The java string startsWith() method checks if this string starts with
startsWith()
given prefix. It returns true if this string starts with given prefix else
returns false.
Java String startsWith() method example
class StartsWith1
{
public static void main(String args[])
{
String s1="akashdeep";
System.out.println(s1.startsWith("ak"));
System.out.println(s1.startsWith("deep"));
}
} Output: true
false
Java String
substring()
The java string substring() method returns a part of the string.
The java string substring() method returns a part of the string.
Java String substring() method example
class Substring1{
public static void main(String args[]){
String s1="ashmit";
System.out.println(s1);
System.out.println(s1.substring(2,4));
System.out.println(s1.substring(2));
}}
Output: ashmit
hm
hmit
Java String toCharArray()
The java string toCharArray() method converts this string into
character array. It returns a newly created character array, its length is
similar to this string and its contents are initialized with the characters of
this string.
Java String toCharArray() method example
class StringToCharArray1{
public static void main(String args[]){
String s1="amandeep";
char[] ch=s1.toCharArray();
for(int i=0;i<ch.length;i++){
System.out.print(ch[i]);
}
}}
Output: amandeep
Java String toLowerCase()
The java string toLowerCase() method returns the string in
lowercase letter. In other words, it converts all characters of the string
into lower case letter.
Java String toLowerCase() method example
class StringLower1
{
public static void main(String args[])
{
String s1="AManDEEP";
String s1lower=s1.toLowerCase();
System.out.println(s1lower);
}
}
Java String toUpperCase()
The java string toUpperCase() method returns the string in
uppercase letter. In other words, it converts all characters of the string
into upper case letter.
Java String toUpperCase() method example
class StringUpper1
{
public static void main(String args[])
{
String s1="amanDEep";
String s1upper=s1.toUpperCase();
System.out.println(s1upper);
}
} Output: AMANDEEP
Java String trim()
The java string trim() method eliminates leading and trailing spaces. The
unicode value of space character is '\u0020'. The trim() method in java
string checks this unicode value before and after the string, if it exists then
removes the spaces and returns the omitted string.
Java String trim() method example
class StringTrim1
{
public static void main(String args[])
{
String s1=" my name is ";
System.out.println(s1+"amandeep");
System.out.println(s1.trim()+"amandeep");
}
}
Java String valueOf()
The java string valueOf() method converts different types of values into
string. By the help of string valueOf() method, you can convert int to
string, long to string, boolean to string, character to string, float to string,
double to string, object to string and char array to string.
Java String valueOf() method example
class StringValueOf1
{
public static void main(String args[])
{
int value=30;
String s1=String.valueOf(value);
System.out.println(s1+10);
}
}
Output:3010