String methods – Concat and contains

In this post, we will see 2 string methods which are  concat and contains.

String concat(String str):

The method appends a specified string to the end of this string.

If the length of the string is 0, after concatenating a new string will return. We can concatenate multiple strings as well as shown in the code below.

public class StringMethodsEx {

	public static void main(String[] args) {
		
		String val1 = "Hello world!!";
		String val2 = "";
		
		System.out.println(val2.concat("New string object"));  // new string
		System.out.println(val1.concat("How ").concat("are ").concat("you."));
	}
}
Output:
New string object
Hello world!!How are you.

boolean contains(CharSequence s):

This method finds the sequence of characters in the any string and returns true if matches ,else false.

public class StringMethodsEx {

	public static void main(String[] args) {
		
		String val1 = "Hello world!!";
		
		System.out.println(val1.contains("Hello"));  //returns true
		System.out.println(val1.contains("Universe"));  //returns false
		
	}
}
Output:
true
false
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...