First, common methods 1
public char charAt (int index) returns the first character in a string
public int Length () returns the lengths of the strings
public int indexOf (string str) returns the first position of STR in a string
public int indexOf (string str, int fromIndex) returns the first position of STR from the string in the beginning of the FromIndex
public boolean equalsignorecase (string another) compares strings with another (ignoring case)
public string Replace (char OldChar, char Newchar) replaces OldChar characters with Newchar characters in strings (replace All)
public boolean startsWith (String str)
public boolean endsWith (String str)
Public String toUpperCase ()
Public String toLowerCase ()
Public String subString (int beginindex)
Public String subString (int beginindex, int endIndex)//Note that the second parameter is not a length OH
public string trim ()//returns the string after which the string is stripped of the beginning and trailing spaces
Ii. Common Methods 2
1. Static Reload Method:
public static String valueOf (...) You can convert the basic type data to a string;
2. Public string[] Split (string regex) can separate a string by a specified delimiter, returning a segmented array of strings
Three, small application: The string of uppercase letters, lowercase letters, and other characters are counted separately
public class StringDemo3 {
public static void Main (string[] args) {
String s = "ABCDDRGTS34FSGFSDTG98VFGATBA";
METHOD1 (s);
METHOD2 (s);
METHOD3 (s);
}
public static void Method1 (String s) {
int ucount = 0, LCount = 0, ocount = 0;
for (int i = 0; i < s.length (); i++) {
char C = S.charat (i);
if (c >= ' a ' && c <= ' z ') {
LCount + = 1;
}
else if (c >= ' A ' && C <= ' Z ') {
Ucount + = 1;
}
else {
Ocount + = 1;
}
}
System.out.println ("Lower count:" + lCount);
System.out.println ("Upper count:" + ucount);
System.out.println ("Other count:" + ocount);
}
public static void Method2 (String s) {
int ucount = 0, LCount = 0, ocount = 0;
String SL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String SU = Sl.touppercase ();
for (int i = 0; i < s.length (); i++) {
char C = S.charat (i);
if (Sl.indexof (c)! =-1) {
LCount + = 1;
}
else if (Su.indexof (c)! =-1) {
Ucount + = 1;
}
else {
Ocount + = 1;
}
}
System.out.println ("Lower count:" + lCount);
System.out.println ("Upper count:" + ucount);
System.out.println ("Other count:" + ocount);
}
public static void Method3 (String s) {
int ucount = 0, LCount = 0, ocount = 0;
for (int i = 0; i < s.length (); i++) {
char C = S.charat (i);
if (Character.islowercase (c)) {
LCount + = 1;
}
else if (Character.isuppercase (c)) {
Ucount + = 1;
}
else {
Ocount + = 1;
}
}
System.out.println ("Lower count:" + lCount);
System.out.println ("Upper count:" + ucount);
System.out.println ("Other count:" + ocount);
}
}
Common methods of the string class