Java, like most programming languages, allows you to use the + sign to join (concatenate) two strings together.
Previous | Home | Next |
Example
//Example for String Concatenation package r4r.co.in; public class StringDemo { public static void main(String args[]) { String s1= new String("He is a man"); String age = "15 year old"; System.out.println(s1.toString()+ " and " + age); //here String is Concatenation } }
Output
He is a man and 15 year old
In String toString()method is used for the Conversion of the String. Java converts data into its string representation during concatenation, it does so by calling one of the overloaded versions of the string conversion method valueOf( ) defined by String.
Example
//program......
In Java package is default java.lang.String package it is subpackage of java.lang contain all the String class and method .The String class in Java contains more than 50 methods.
String Method whose mostly used
- char charAt(int index): returns the character at the specified location.
- int compareTo(String other): returns a negative value if the string comes before other in dictionary order, a positive value if the string comes after other in dictionary order, or 0 if the strings are equal.
- boolean endsWith(String suffix): returns true if the string ends with suffix.
- boolean equals(Object other) returns true if the string equals other.
- boolean equalsIgnoreCase(String other): returns true if the string equals other, except for upper/lowercase distinction.
- int indexOf(String str)
- int indexOf(String str, int fromIndex): return the start of the first substring equal to str, starting at index 0 or at fromIndex.
- int lastIndexOf(String str)
- int lastIndexOf(String str, int fromIndex) return the start of the last substring equal to str, starting at index 0 or at fromIndex.
- int length(): returns the length of the string.
- String replace(char oldChar, char newChar): returns a new string that is obtained by replacing all characters oldChar in the string with newChar.
- boolean startsWith(String prefix):returns true if the string begins with prefix.
- String substring(int beginIndex)
- String substring(int beginIndex, int endIndex): return a new string consisting of all characters from beginIndex until the end of the string or until endIndex (exclusive).
- String toLowerCase(): returns a new string containing all characters in the original string, with uppercase characters converted to lower case.
- String toUpperCase(): returns a new string containing all characters in the original string, with lowercase characters converted to upper case.
- String trim(): returns a new string by eliminating all leading and trailing spaces in the original string.
Previous | Home | Next |