A string in Java is also a series of characters. But unlike many other computer languages that handle strings as character arrays, Java handles strings as String type objects. Treating strings as built-in objects allows Java to provide a very rich set of features for handling strings. Below are some frequently used functions and their related descriptions.
String related functions
1)substring()
It has two forms, the first is: String substring(int startIndex)
The second one is: String substring(int startIndex,int endIndex)
2) concat() connects two strings. Example: String s="Welcome to ";
String t=s.concat("AnHui");
3)replace() has two forms of replacement. The first form uses a character to replace all places where a certain character appears in the calling string. The form is as follows:
String replace(char original,char replacement)
For example: String s="Hello".replace('l','w');
The second form is to replace one character sequence with another character sequence, as follows:
String replace(CharSequence original,CharSequence replacement)
4)trim() removes starting and ending spaces
5)valueOf() convert to string
6)toLowerCase() converts to lowercase
7)toUpperCase() converts to uppercase
8)length() obtains the length of a string. Example: char chars[]={'a','b'.'c'};
String s=new String(chars);
int len=s.length();
9) charAt() intercepts a character example: char ch;
ch=”abc”.charAt(1);
The return value is 'b'
10)getChars() intercepts multiple characters
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart specifies the subscript of the starting character of the substring
sourceEnd specifies the index of the next character after the end of the substring. Therefore, the substring contains characters from sourceStart to sourceEnd-1.
target specifies the array to receive characters
targetStart The subscript value to start copying the substring in the target. Example: String s="this is a demo of the getChars method.";
char buf[]=new char[20];
s.getChars(10,14,buf,0);
11)getBytes()
An alternative to getChars() is to store the characters in a byte array, which is getBytes()
example:
String s = "Hello! Hello!";
byte[] bytes = s.getBytes();
12)toCharArray()
example:
String s = "Hello! Hello!";
char[] ss = s.toCharArray();
13)equals() and equalsIgnoreCase() compare two strings
14)regionMatches() is used to compare a specific region in a string with another specific region. It has an overloaded form that allows ignoring case in the comparison.
boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String
str2,int str2StartIndex,int numChars)
15) startsWith() and endsWith()
startsWith() method determines whether to start with a specific string
The endWith() method determines whether to end with a specific string
16)equals() and ==
The equals() method compares characters in string objects
The == operator compares whether two objects refer to the same instance.
Example: String s1="Hello";
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false
17)compareTo() and compareToIgnoreCase() compare strings
18)indexOf() and lastIndexOf()
indexOf() finds the first occurrence of a character or substring.
lastIndexOf() finds the last occurrence of a character or substring.
19) Example of trim function to remove spaces: String t1 = " abc de ";
System.out.println(t1.trim());//Remove the leading and trailing spaces "abc de"
20)split string splitting
String y = "abc,de,fg,hi,jk";
String[] y1 = y.split(",");//Intercept all "," characters in the string
for (int i = 0; i < y1.length; i++) {
System.out.print(y1[i]);//output result abcdefghijk
}
21)append adds or inserts a function
StringBuffer zz1 = new StringBuffer(z1); // append insert characters
zz1.append('|').append("hijk").append('/').append("lmn").append("opq");
System.out.println();
System.out.print(zz1);//Output: abcdefg|hijk/lmnopq
StringBuffer constructor
StringBuffer defines three constructors:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
The following are StringBuffer related functions:
1)length() and capacity()
The current length of a StringBuffer can be obtained through the length() method, and the entire allocable space can be obtained through the capacity() method.
2) ensureCapacity() sets the buffer size
void ensureCapacity(int capacity)
3)setLength() sets the length of the buffer
void setLength(int len)
4)charAt() and setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)
5)getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
6) append() can connect the string representation of any type of data to the end of the calling StringBuffer object.
Example: int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append("a=").append(a).append("!").toString();
6)insert() inserts a string
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
7)index specifies the subscript of the position where the string is inserted into the StringBuffer object.
8)reverse() reverses the characters in the StringBuffer object
StringBuffer reverse()
9)delete() and deleteCharAt() delete characters
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)
10)replace() replace
StringBuffer replace(int startIndex,int endIndex,String str)
11)substring() intercepts substring
String substring(int startIndex)
String substring(int startIndex,int endIndex)
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/hzy20090501/archive/2009/12/30/5103817.aspx
-