Java String startsWith

The java string startsWith()method checks if this string starts with given prefix. It returns true if this string starts with given prefix else returns false.

Signature

The syntax or signature of startWith() method is given:

public boolean startsWith(String prefix)  
public boolean startsWith(String prefix, int offset)   

Parameter

prefix:Sequence of character

Returns

true or false.

Java String startsWith() method example

public class StartsWithExample{  
public static void main(String args[]){  
String s1="java string split method by catchmecoder";  
System.out.println(s1.startsWith("ja"));  
System.out.println(s1.startsWith("java string"));  
}}

Output:

true
true

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.

Signature

The signature or syntax of string toCharArray() method is given:

public char[] toCharArray()

Returns

character array

Java String toCharArray() method example

public class StringToCharArrayExample{  
public static void main(String args[]){  
String s1="hello";  
char[] ch=s1.toCharArray();  
for(int i=0;i< ch.length;i++){  
System.out.print(ch[i]);  
}  
}}        

Output:

hello

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.

Signature

The signature or syntax of string trim method is given:

public String trim()

Returns

string with omitted leading and trailing spaces

Java String trim() method example

public class StringTrimExample{  
public static void main(String args[]){  
String s1="  hello string   ";  
System.out.println(s1+"catchmecoder"); //without trim()  
System.out.println(s1.trim()+"catchmecoder");  //with trim()  
}}      

Output:

 hello string   catchmecoder
hello stringcatchmecoder