Stirng toString() in Java

toString(): This method is available in java.lang.String class of Java.

Syntax:

String java.lang.String.toString()

This object (which is already a string!) is itself returned.

Returns: the string itself.

For Example:

String str = "hello"

str.toString() = > It returns hello.

Approach

Java

public class ToString {
    public static void main(String[] args) {

        String str = "hello";

        System.out.println(str.toString());
    }
}

Output:

hello


Some more methods of String

charAt() It returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

codePointAt()It returns the character (Unicode code point) at the specified index. The index refers to char values(Unicode code units) and ranges from 0 to length() - 1.

codePointBefore()It returns the character (Unicode code point) before the specified index.

codePointCount()It returns the number of Unicode code points in the specified text range of this String.

compareTo()It compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

compareToIgnoreCase()It compares two strings lexicographically, ignoring case differences.

concat()It concatenates the specified string to the end of this string.

contains()It returns true if and only if this string contains the specified sequence of char values.

contentEquals(CharSequence cs): It compares this string to the specified CharSequence.

contentEquals(StringBuffer sb) It compares this string to the specified StringBuffer.

String.copyValueOf(char[] data)It returns the String representation of the char array characters.

String.copyValueOf(char[] data, int offset, int count) It returns the string that contains the character of the specified sub-array.

endsWith()It tests if this string ends with the specified suffix.

equals()It compares this string to the specified object.

equalsIgnoreCase()It Compares this String to another String, ignoring case considerations.

String.format()It returns a formatted string using the specified format string and arguments.

formatted()It formats using this string as the format string, and the supplied arguments.

String.format(Locale l, String format, Object... args): It returns a formatted string using the specified locale, format string, and arguments.

getBytes()It encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

getBytes(Charset charset): It encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array. 

getBytes(String charsetName)It encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

hashCode()It returns a hash code for this string.

indent()It adjusts the indentation of each line of this string based on the value of n and normalizes line termination characters.

indexOf(int ch)It returns the index within this string of the first occurrence of the specified character. 

indexOf(int ch, int fromIndex)It returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

indexOf(String str)It returns the index within this string of the first occurrence of the specified substring.

indexOf(String str, int fromIndex)It returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

intern()It returns a canonical representation for the string object.

isBlank()It returns true if the string is empty or contains only white space code points, otherwise false.

isEmpty()It returns true if, and only if, length() is 0.

String.join(): It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

String.join(CharSequence delimiter, Iterable<? extends CharSequence> elements)It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

lastIndexOf(int ch) It returns the index within this string of the last occurrence of the specified character.

lastIndexOf(int ch, int fromIndex)It returns the index within this string of the last occurrence of the specified character.

lastIndexOf(String str)It returns the index within this string of the last occurrence of the specified substring.

lastIndexOf(String str, int fromIndex): It returns the index within this string of the last occurrence of the specified substring.

length()It returns the length of this string.

matches()It tells whether or not this string matches the given regular expression.

offsetByCodePoints():It returns the index within this String that is offset from the given index by codePointOffset code points.

regionMatches(int toffset, String other, int ooffset, int len)It tests if two string regions are equal.

regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)It tests if two string regions are equal. A substring of this String object is compared to a substring of the argument other.

repeat()It returns a string whose value is the concatenation of this string repeated count times.

replaceAll()It replaces each substring of this string that matches the given regular expression with the given replacement.

replace(char oldChar, char newChar) It returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

replace(CharSequence target, CharSequence replacement)It replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

replaceFirst()It replaces the first substring of this string that matches the given regular expression with the given replacement.

split(String regex)It splits this string around matches of the given regular expression.

split(String regex, int limit)It splits this string around matches of the given regular expression.

startsWith(String prefix)It tests if this string starts with the specified prefix.

startsWith(String prefix, int offset)It tests if the substring of this string beginning at the specified index starts with the specified prefix.

strip()It returns a string whose value is this string, with all leading and trailing white space removed.

stripIndent()It returns a string whose value is this string, with incidental white space removed from the beginning and end of every line.

stripLeading()It returns a string whose value is this string, with all leading white space removed.

stripTrailing()It returns a string whose value is this string, with all trailing white space removed.

subSequence() It returns a character sequence that is a subsequence of this sequence.

substring(int beginIndex)It returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

substring(int beginIndex, int endIndex)It returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex -1.

toCharArray()It converts this string to a new character array.

toLowerCase()It converts all of the characters in this String to lowercase using the rules of the default locale.

toLowerCase(Locale locale)It converts all of the characters in this String to lowercase using the rules of the given Locale.

toString()This object (which is already a string!) is itself returned.

toUpperCase()This method converts all of the characters in this String to uppercase using the rules of the default locale.

toUpperCase(Locale locale)This method converts all of the characters in this String to uppercase using the rules of the given Locale. Case mapping is based on the Unicode Standard version specified by the Character class.

translateEscapes()This method returns a string whose value is this string, with escape sequences translated as if in a string literal.

trim()This method returns a string whose value is this string, with all leading and trailing space removed.

String.valueOf(boolean b)This method returns the string representation of the boolean argument.

String.valueOf(char c) This method returns the string representation of the char argument.

String.valueOf(char[] data) This method returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the returned string.

String.valueOf(char[] data, int offset, int count)his method returns the string representation of a specific subarray of the char array argument.

String.valueOf(double d)This method returns the string representation of the double argument.

String.valueOf(float f)This method returns the string representation of the float argument.

String.valueOf(int i)This method returns the string representation of the int argument.

String.valueOf(long l)This method returns the string representation of the long argument.

No comments:

Post a Comment