string java | new string java | string in java | java string example | string class methods in java
As most other programming languages, in java string is a sequence of characters. Java implements string as objects of type String. String class is to create string object.How to create String in Java: -
- Implicitly (By string literal) created string in java
- Explicitly (By new keyword) created string in java
String s="Hello world";
Explicitly (By new keyword) created string in java: - String create in java, when we use the new operator to instantiate a String object. For example:
String s= new String("Hello world");
Example of String in Java: -
- Example of Implicitly Creating String in Java
- Example of Explicitly Creating String in Java
String Class Methods in Java
- charAt( ): -It returns the character at the specified index.
- compareTo( ): -It compares this String to another Object.
- concat( ): -Concatenates the specified string to the end of this string.
- equals( ): -It compares this string to the specified object.
- endsWith( ): -Tests if this string ends with the specified suffix.
- getBytes( ): -Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
- getChars( ): -It copies characters from this string into the destination character array.
- length( ): -It returns the length of this string.
- indexOf( ): -It returns the index within this string of the first occurrence of the specified character.
- lastIndexOf( ): -It returns the index within this string of the last occurrence of the specified character.
- replace( ): -It returns a new string resulting from replacing all occurrences of old-Character in this string with new-Character.
- split( ): -Splits this string around matches of the given regular expression.
- substring( ): -It returns a new string that is a substring of this string.
- toCharArray( ): -It converts this string to a new character array.
- startsWith( ): -Tests if this string starts with the specified prefix.
- toLowerCase( ): -It converts all of the characters in this String to lower case using the rules of the default locale.
- toUpperCase( ): -It converts all of the characters in this String to upper case using the rules of the default locale.
- trim( ): -It returns a copy of the string, with leading and trailing whitespace omitted.
- toString( ): -This object (which is already a string!) is returned itself.
- replaceAll( ): -It replaces each substring of this string that matches the given regular expression with the given replacement.