Strings are widely used in Java programming. Strings are objects in Java, and Java provides String classes for creating and manipulating strings.
Create a string
The simplest way to create a string is as follows:
String greeting = " newbie tutorial " ;
When a string constant is encountered in the code, the value here is a " newbie tutorial " and the compiler uses that value to create a String object.
Like other objects, you can create a String object using keywords and constructors.
The String class has 11 constructors that provide different parameters to initialize the string, such as providing a character array parameter:
StringDemo.java file code:
public class StringDemo{
public static void main(String args[]){
char[] helloArray = { 'h', 'e', 'l', 'l', 'o'};
String helloString = new String(helloArray);
System.out.println( helloString );
}
}
The above example compiled and run results are as follows:
hello
Note: The String class is immutable, so once you create a String object, its value cannot be changed (see the notes section for details).
If you need to make many changes to the string, you should choose to use the StringBuffer & StringBuilder class .
String length
The method used to get information about an object is called an accessor method.
An accessor method of the String class is the length() method, which returns the number of characters contained in a string object.
After the following code is executed, the len variable is equal to 14:
StringDemo.java file code:
public class StringDemo {
public static void main ( String args [ ] ) {
String Site = "interviewbubble.com" ;
int len = Site.length ( ) ;
System.out.println( " Rookie Tutorial URL Length: " + len ) ;
} }
The above example compiled and run results are as follows:
Rookie Tutorial URL Length : 19
Connection string
The String class provides methods for concatenating two strings:
string1.concat(string2);
Returns the new string of string2 connected to string1. You can also use the concat() method on string constants, such as:
"My name is" . concat ( "bubble" );
More commonly used is the use of the '+' operator to concatenate strings, such as:
"Hello," + " bubble" + "!"
The result is as follows:
"Hello, bubble!"
Below is an example:
StringDemo.java file code:
public class StringDemo {
public static void main(String args[]) {
String string1 = "Rookie tutorial URL:";
System.out.println("1、" + string1 + "www.interviewbubble.com");
}
}
The above example compiled and run results are as follows:
1 , Rookie tutorial URL: www.interviewbubble.com
Create a format string
We know that output formatting numbers can use the printf() and format() methods.
The String class uses the static method format() to return a String object instead of a PrintStream object.
The static method format() of the String class can be used to create reusable format strings, not just for a printout.
As follows:
System.out.printf ( " The value of the float variable " +
" %f, The value of the integer variable is " +
" %d, The value of the string variable is " +
" is %s " , floatVar , intVar , stringVar ) ;
You can also write like this
String fs ;
fs = String.format ( " value of float variables " +
" % F, the value of the integer variable " +
" % D, of the string variable value " +
" % S " , floatVar , intVar , stringVar ) ;
String method
Here are the methods supported by the String class. For more details, see the Java String API documentation:
SN (serial number) | Method description |
---|---|
1 | Char charAt(int index) Returns the char value at the specified index. |
2 | Int compareTo(Object o) Compares this string with another object. |
3 | Int compareTo(String anotherString) Compares two strings lexicographically. |
4 | Int compareToIgnoreCase(String str) Compares two strings lexicographically, regardless of case. |
5 | String concat(String str) Concatenates the specified string to the end of this string. |
6 | Boolean contentEquals(StringBuffer sb) Returns true if and only if the string has characters in the same order as the specified StringBuffer. |
7 | Static String copyValueOf(char[] data) Returns a String representing the sequence of characters in the specified array. |
8 | Static String copyValueOf(char[] data, int offset, int count) Returns a String representing the sequence of characters in the specified array. |
9 | Boolean endsWith(String suffix) Tests if this string ends with the specified suffix. |
10 | Boolean equals(Object anObject) Compares this string with the specified object. |
11 | Boolean equalsIgnoreCase(String anotherString) Compares this String with another String, regardless of case. |
12 | Byte[] getBytes() Encodes this String into a byte sequence using the platform's default character set and stores the result in a new byte array. |
13 | Byte[] getBytes(String charsetName) Encodes this String into a byte sequence using the specified character set and stores the result in a new byte array. |
14 | Void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Copies characters from this string to the target character array. |
15 | Int hashCode() Returns the hash code for this string. |
16 | Int indexOf(int ch) Returns the index of the first occurrence of the specified character in this string. |
17 | Int indexOf(int ch, int fromIndex) Returns the index at the first occurrence of the specified character in this string, starting at the specified index. |
18 | Int indexOf(String str) Returns the index of the first occurrence of the specified substring in this string. |
19 | Int indexOf(String str, int fromIndex) Returns the index of the first occurrence of the specified substring within this string, starting at the specified index. |
20 | String intern() Returns a normalized representation of a string object. |
21 | Int lastIndexOf(int ch) Returns the index of the last occurrence of the specified character in this string. |
22 | Int lastIndexOf(int ch, int fromIndex) Returns the index of the last occurrence of the specified character in this string, starting at the specified index. |
23 | Int lastIndexOf(String str) Returns the index of the specified substring at the rightmost occurrence of this string. |
24 | Int lastIndexOf(String str, int fromIndex) Returns the index of the last occurrence of the specified substring in this string, starting from the specified index. |
25 | Int length() Returns the length of this string. |
26 | Boolean matches(String regex) tells whether this string matches the given regular expression. |
27 | Boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) Tests if two string regions are equal. |
28 | Boolean regionMatches(int toffset, String other, int ooffset, int len) Tests if two string regions are equal. |
29 | String replace(char oldChar, char newChar) returns a new string, which is obtained by replacing all occurrences of oldChar in this string with newChar. |
30 | String replaceAll(String regex, String replacement) Replaces all substrings of this string that match the given regular expression with the given replacement. |
31 | String replaceFirst(String regex, String replacement) Replaces the first substring of the given regular expression with the given replacement. |
32 | String[] split(String regex) Splits this string according to the match of the given regular expression. |
33 | String[] split(String regex, int limit) Splits this string according to the given regular expression. |
34 | Boolean startsWith(String prefix) Tests if this string starts with the specified prefix. |
35 | Boolean startsWith(String prefix, int toffset) Tests if the substring beginning with the specified index starts with the specified prefix. |
36 | CharSequence subSequence(int beginIndex, int endIndex) returns a new character sequence, which is a subsequence of this sequence. |
37 | String substring(int beginIndex) returns a new string, which is a substring of this string. |
38 | String substring(int beginIndex, int endIndex) returns a new string, which is a substring of this string. |
39 | Char[] toCharArray() Converts this string to a new character array. |
40 | String toLowerCase() Converts all characters in this String to lowercase using the rules of the default locale. |
41 | String toLowerCase(Locale locale) Converts all characters in this String to lowercase using the rules of the given Locale. |
42 | String toString() Returns this object itself (it is already a string!). |
43 | String toUpperCase() Converts all characters in this String to uppercase using the rules of the default locale. |
44 | String toUpperCase(Locale locale) Converts all characters in this String to uppercase using the rules of the given Locale. |
45 | String trim() returns a copy of the string, ignoring leading blanks and trailing blanks. |
46 | Static String valueOf(primitive data type x) Returns a string representation of the x parameter of the given data type. |
Comments
Post a Comment