Java String

Java String is a very important topic in Java. It’s basically an object, that represent sequence of char values. Also, you can create String objects by using the new keyword and a constructor.

char[] ch={‘r’,’o’,’a’,’d’,’t’,’o’,’s’,’d’,’e’,’t’};
String string=new String(ch);

is same as

String string=”roadtosdet”;

Example 1:
public class Main {
    public static void main(String[] args) {
        char[] ch={'r','o','a','d','t','o','s','d','e','t'};
        String arrayString=new String(ch);
        System.out.println(arrayString);
        String string="roadtosdet";
        System.out.println(string);
    }

}

Example 2:

public class Main {
    public static void main(String[] args) {
        char[] ch={'r','o','a','d','t','o','s','d','e','t'};
        String arrayString=new String(ch); //creating character object
        System.out.println(arrayString);
        String string1="roadtosdet"; //assign string value to variable
        System.out.println(string1);
        String string2=new String(string1); //creating String object
        System.out.println(string2);
    }

}

Java String methods are here:

MethodTypeDescription
charAt()charReturns the character at the specified index (position)
codePointAt()intReturns the Unicode of the character at the specified index
codePointBefore()intReturns the Unicode of the character before the specified index
codePointCount()intReturns the Unicode in the specified text range of this String
compareTo()intCompares two strings lexicographically
compareToIgnoreCase()intCompares two strings lexicographically, ignoring case differences
concat()StringAppends a string to the end of another string
contains()booleanChecks whether a string contains a sequence of characters
contentEquals()booleanChecks whether a string contains the exact same sequence of characters
copyValueOf()StringReturns a String that represents the characters of the character array
endsWith()booleanChecks whether a string ends with the specified character
equals()booleanCompares two strings. Returns true if the strings are equal, and false if not
equalsIgnoreCase()booleanCompares two strings, ignoring case considerations
format()StringReturns a formatted string using the specified locale, format string, and arguments
getBytes()byte[]Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array
getChars()voidCopies characters from a string to an array of chars
hashCode()intReturns the hash code of a string
indexOf()intReturns the position of the first found occurrence of specified characters in a string
intern()StringReturns the index within this string of the first occurrence of the specified character, starting the search at the specified index
isEmpty()booleanChecks whether a string is empty or not
lastIndexOf()intReturns the position of the last found occurrence of specified characters in a string
length()intReturns the length of a specified string
matches()booleanSearches a string for a match against a regular expression, and returns the matches
offsetByCodePoints()intReturns the index within this String that is offset from the given index by codePointOffset code points
regionMatches()booleanTests if two string regions are equal
replace()StringSearches a string for a specified value, and returns a new string where the specified values are replaced
replaceFirst()StringReplaces the first occurrence of a substring that matches the given regular expression with the given replacement
replaceAll()StringReplaces each substring of this string that matches the given regular expression with the given replacement
split()String[]Splits a string into an array of substrings
startsWith()booleanChecks whether a string starts with specified characters
subSequence()charReturns a new character sequence that is a subsequence of this sequence
substring()StringExtracts the characters from a string, beginning at a specified start position, and through the specified number of character
toCharArray()char[]Converts this string to a new character array
toLowerCase()StringConverts a string to lower case letters
toString()StringConverts a string to lower case letters
toUpperCase()StringConverts a string to upper case letters
trim()StringRemoves whitespace from both ends of a string
valueOf()StringReturns the primitive value of a String object

Here are some examples.

about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *