You are on page 1of 8

String methods

concat( )
Description Syntax Return value
combines specified string <string_obj>.concat(str_arg) string
at the end of this string

class Sample{
public static void main(String [ ] args){
String str = “Hello”;

str.concat(“ to Java World”);


System.out.println(str);
}}
contains( )
Description Syntax Return value
searches the sequence of <string_obj>.contains(str_to_search) TRUE, if sequence
characters in the given of char values are
string found in given
string
FALSE, if
sequence of char
values are not
found in given
string
contains( )
class Sample{
public static void main(String [ ] args){ Output:
String str = “The fox is red.”; true

System.out.println(str.contains(“red”));

}}
join( )
Description Syntax Return value
method returns a string String <string_obj> = String
joined with given String.join(<delimiter>,<element1>,
delimiter <element2>…<elementn>)

class Sample{
public static void main(String [ ] args){
String str = String.join(“-”,”The”,”red”,”fox”);

System.out.println(str);

}}

Output:
The-red-fox
isEmpty( )
Description Syntax Return value
checks if this string is <string_object>.isEmpty( ); It returns true, if
empty or not length of string is 0
otherwise false.

class Sample{
public static void main(String [ ] args){ Output:
String str = “”; true
String str2 = “Hello to Java World”;

System.out.println(str.isEmpty());
System.out.println(str2.isEmpty());

}}
toUpperCase( )
Description Syntax Return value
converts all characters of <string_object>.isEmpty( ); String
the string into upper case
letter.

class Sample{
public static void main(String [ ] args){ Output:
String str = “Hello to Java World”; HELLO TO JAVA WORLD

System.out.println(str.toUpperCase());

}}
trim( )
Description Syntax Return value
eliminates leading and <string_object>.trim( ); String
trailing spaces

class Sample{
public static void main(String [ ] args){ Output:
String str =" hello to java world! ";
hello to java world!
hello to java world!
System.out.println(str);
System.out.println(str.trim());

}}