11
Dec
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:
Method | Type | Description |
charAt() | char | Returns the character at the specified index (position) |
codePointAt() | int | Returns the Unicode of the character at the specified index |
codePointBefore() | int | Returns the Unicode of the character before the specified index |
codePointCount() | int | Returns the Unicode in the specified text range of this String |
compareTo() | int | Compares two strings lexicographically |
compareToIgnoreCase() | int | Compares two strings lexicographically, ignoring case differences |
concat() | String | Appends a string to the end of another string |
contains() | boolean | Checks whether a string contains a sequence of characters |
contentEquals() | boolean | Checks whether a string contains the exact same sequence of characters |
copyValueOf() | String | Returns a String that represents the characters of the character array |
endsWith() | boolean | Checks whether a string ends with the specified character |
equals() | boolean | Compares two strings. Returns true if the strings are equal, and false if not |
equalsIgnoreCase() | boolean | Compares two strings, ignoring case considerations |
format() | String | Returns 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() | void | Copies characters from a string to an array of chars |
hashCode() | int | Returns the hash code of a string |
indexOf() | int | Returns the position of the first found occurrence of specified characters in a string |
intern() | String | Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index |
isEmpty() | boolean | Checks whether a string is empty or not |
lastIndexOf() | int | Returns the position of the last found occurrence of specified characters in a string |
length() | int | Returns the length of a specified string |
matches() | boolean | Searches a string for a match against a regular expression, and returns the matches |
offsetByCodePoints() | int | Returns the index within this String that is offset from the given index by codePointOffset code points |
regionMatches() | boolean | Tests if two string regions are equal |
replace() | String | Searches a string for a specified value, and returns a new string where the specified values are replaced |
replaceFirst() | String | Replaces the first occurrence of a substring that matches the given regular expression with the given replacement |
replaceAll() | String | Replaces 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() | boolean | Checks whether a string starts with specified characters |
subSequence() | char | Returns a new character sequence that is a subsequence of this sequence |
substring() | String | Extracts 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() | String | Converts a string to lower case letters |
toString() | String | Converts a string to lower case letters |
toUpperCase() | String | Converts a string to upper case letters |
trim() | String | Removes whitespace from both ends of a string |
valueOf() | String | Returns the primitive value of a String object |
Here are some examples.
Leave a Reply