Common methods of the string class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.