Page Contents
Function Description
Java String class concat() method is used to combine the specified string at the end of the string. This method returns the concatenated string if specified string’s length is greater than zero otherwise it returns the same string.
For Example:
If String in which we want to combine the string is “Hello”
String to be concatenated is “” blank, then returned concatenated string will be “Hello”
If the String to be concatenated is “ Test”, then returned concatenated string will be “Hello Test”
Method Signature
public String concat(String str)
Parameter
str : A string to be concatenated at the end of the string
Return Type
A Concatenated String
Exception
Throws NullPointerException if the specified string to be concatenated is null(the str parameter is null)
Example of Java String Class concat() Method
public class ExampleStringConcat { public static void main(String[] args) { //String in which we want to concat another string String strHello = "Hello!"; //String to be concatenated String strTech = "RoundTheTech"; String concatStr = strHello.concat(strTech); System.out.println("String in which another string will be concatenated : "+strHello); System.out.println("String to be concatenated : "+strTech); System.out.println("After concat : "+concatStr); String str= "Welcome to String concat function "; //another way to use concat concatStr = strHello.concat(strTech).concat(". ").concat(str); System.out.println("\nString after concat : " + concatStr); } } Output: String in which another string will be concatenated : Hello! String to be concatenated : RoundTheTech After concat : Hello! RoundTheTech String after concat : Hello! RoundTheTech. Welcome to String concat function
Example when NullPointer Exception is Thrown
public class ExampleStringConcat { public static void main(String[] args) { //String in which we want to concat another string String strHello = "Hello!"; //String to be concatenated. It is null String strTech = null; //strTech is null, so concat() method will throw NullPointerException String concatStr = strHello.concat(strTech); System.out.println("String in which another string will be concatenated : "+strHello); System.out.println("String to be concatenated : "+strTech); System.out.println("After concat : "+concatStr); String str= "Welcome to String concat function "; //another way to use concat concatStr = strHello.concat(strTech).concat(". ").concat(str); System.out.println("\nString after concat : " + concatStr); } } Output: Exception in thread "main" java.lang.NullPointerException at java.lang.String.concat(String.java:2027) at com.java.test.ExampleStringConcat.main(ExampleStringConcat.java:11)
Other Methods of String Class
Sequence | Method | Description |
1 | char charAt(int index) | This method is used to return the character of the string at specified index. First character is at index 0 while last is at string length-1 . StringIndexOutOfBounds Exception is thrown if valid index value is not passed. |
2 | int length() | Returns the number of characters count in a string. |
3 | static String format(String format, Object… args) | Return the formatted String. It is a static method and formats the string as per passing format and argument(s). |
4 | static String format(Locale l, String format, Object… args) | Return the formatted String. It is a static method and formats the string as per passing format and argument(s) and locale. |
5 | String substring(int beginIndex) | Returns substring from the given index position to the end index of the string. |
6 | String substring(int beginIndex, int endIndex) | Returns substring from the specified begin index to the specified end index. |
7 | boolean contains(CharSequence s) | Returns boolean value(true/false). It searches the given sequence of characters within the string and returns true/false accordingly. Characters will be case sensitive while searching in string. |
8 | static String join(CharSequence delimiter, CharSequence… elements) | Returns joined string. This method joins each passing String using the specified delimiter. It is a static method. |
9 | static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) | Returns the joined string. This method joins each passing string object of an iterate object using the specified delimiter and returns the concatenated string. It is a static method. |
10 | boolean equals(Object another) | Returns boolean value(true/false). It checks the equality of string with the passing object. |
11 | boolean isEmpty() | Returns boolean value(true/false). It returns true if the string is empty otherwise returns false. |
12 | String replace(char old, char new) | Return the replaced string. It replaces all occurrences of the specified character with the new character. |
13 | String replace(CharSequence old, CharSequence new) | Return the replaced string. It replaces all occurrences of the specified Character sequence with the specified new Character sequence. |
14 | String replaceAll(String regex, String replacement) | Returns the replaced string. It replaces all occurrences of the sequence of characters with the given replacement string that are matching with the given regular expression. |
15 | String replaceFirst(String regex, String replacement) | Returns the replaced string. It replaces the first matching sequence of characters with the replacement string that matches with the given regular expression. |
16 | static boolean equalsIgnoreCase(String another) | Returns the boolean (true/false) value. It compares the passing string with string by ignoring the case. It compares the content irrespective of lowercase or uppercase of the character(s). |
17 | String[] split(String regex) | Returns the array of strings resulting from splitting of the string around matches of the given regular expression. String is breaked into strings from all the matching regular expressions. |
18 | String[] split(String regex, int limit) | Returns the array of strings resulting from splitting of the string around matches of the given regular expression. It breaks the string from all the matching regular expression upto the given limit. |
19 | String intern() | Returns an interned string. This method helps to compare two objects using == operator and checks in a string constant pool. it is faster for comparison. |
20 | int indexOf(int ch) | Returns the index value of first occurrence of the specified character within the string |
21 | int indexOf(int ch, int fromIndex) | Returns the index value of first occurrence of the specified character from the substring starting from specified index |
22 | int indexOf(String substring) | Returns the index value of first occurrence of the specified substring within the string. |
23 | int indexOf(String substring, int fromIndex) | Returns the index value of the first occurrence of the specified string within the substring starting from specified index to end of the string index. |
24 | int lastIndexOf(int ch) | Returns the index value of last occurrence of the specified character |
25 | int lastIndexOf (int ch, int fromIndex) | Returns the index value of last occurrence of the specified character, search starts at given fromIndex. |
26 | int lastIndexOf (String str) | Returns the index value of last occurrence of the specified string. |
27 | int lastIndexOf (String str, int fromIndex | Returns the index value of last occurrence of the specified string, searches start from given fromIndex |
28 | String toLowerCase() | Returns all the characters of the string in lowercase . |
29 | String toLowerCase(Locale l) | Returns all the characters of the string in lowercase using specified locale. |
30 | String toUpperCase() | Returns all the characters of the string in uppercase. |
31 | String toUpperCase(Locale l) | Returns all the characters of the string in uppercase using specified locale. |
32 | String trim() | Returns the string after removing the white space from the beginning and end of the string. |
33 | static String valueOf(int value) | Return the converted string. It converts the objects and all primitive types of values into string. It is a static method. |
In this article, we learned all about the concat() method of String class with examples. Apart from the concat() method, we got an idea about other String methods too. We will come up with more articles explaining other String methods description with examples. For more details please click on the respective methods link.
Very informative blog article.Much thanks again. Fantastic.
Thanks again for the article. Really Cool.
Thanks so much for the blog post.Thanks Again. Great.
I like a very useful article, I like our page and follow it
I don’t even know the way I ended up right here, but I thought this publish was
good. I don’t recognize who you might be but definitely you are going to a well-known blogger should
you are not already. Cheers!
I am actually pleased to read this web site posts which contains tons of useful information, thanks for providing
these statistics.
Nice blog here! Also your website loads up fast! What host are you using?
Can I get your affiliate link to your host? I wish my site loaded up as fast as yours lol