Methods commonly used Java String class

length()

Returns the length of the string object.

String string = "abcd";
int len = string.length();
System.out.println(len);

Operating results as follows:

4

charAt()

char charAt (int index) Returns the char value at the specified index (i.e., a character taken).

String str = "hello world";
char ch = str.charAt(6);
System.out.println(ch);

Operating results as follows:

w

compareTo()

String class the compareTo () method returns the result of the comparison of two strings objects. If before this String object lexicographically parameter string is located, the comparison result is a negative integer, then if this String object lexicographically parameter string is located, the comparison result is a positive integer. If equal, it returns 0.
When not equal, the two start comparing the first character string, returns the first character is not equal to the difference. If the front part of a longer string happens to be shorter string, return to their length difference.

String s1 = "abc";
String s2 = "abcd";
String s3 = "abcdfg";
String s4 = "1bcdfg";
String s5 = "b";
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
System.out.println(s1.compareTo(s4));//48 ("a"的ASCII码是97,"1"的的ASCII码是49,所以返回48)
System.out.println(s1.compareTo(s5));//-1 ("a"的ASCII码是97,"b"的ASCII码是98,所以返回-1)

Operating results as follows:

-1
-3
48
-1

substring()

There are two substring () method in the String class, first as follows:

substring(int start)

Wherein the start index is taken to be the start position, the method returns a string, the content of the original string, starting from start to end of the original string of the intermediate data.
If start is greater than the length of the string, bounds exception is thrown.

String str = "hello word!";
System.out.println(str.substring(1));
System.out.println(str.substring(3));
System.out.println(str.substring(6));

Operating results as follows:

ello word!
lo word!
word!

The second substring () method:

substring(int start, int end)

Wherein, the index start position is the start, end to end until the index, the method returns a string, the content of the original string, starting from start to the end position of the end of the intermediate data.
Note: Including start position, but does not include end position .

String str = "hello word!";
System.out.println(str.substring(1, 4));
System.out.println(str.substring(3, 5));
System.out.println(str.substring(0, 4));

Operating results as follows:

ell
lo
hell

indexOf(),lastIndexOf()

the indexOf () method is used to find the object class String substring, the method returns an integer value, as the start position of the substring, if the presence of a plurality of substrings, returns the smallest integer value; If the sub character not found string, return -1.

String str1 = "abcdefghijklmnabc";
System.out.println(str1.indexOf("c"));
System.out.println(str1.indexOf("x"));

Operating results as follows:

2
-1

the indexOf () method can have two parameters, can search from a designated location.
lastIndexOf () method returns the substring of the position of the last occurrence.

equals()

To determine if the java two basic types of data are equal, using a double equal sign, for example: 1 is equal to 1, 1 == 1 is used, and determines whether the same character string using the equals method requires, because each string may have different memory addresses, and == determine is whether the same memory address. And there are a lot of cases, two strings of memory addresses are different, but the value of the string are the same, so use double equal sign can not accurately verify whether they are the equal.

String str1 = new String("张三");
String str2 = new String("张三");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));

Operating results as follows:

false
true

split()

The given regular expression matching split this string.
Special circumstances:
|. ^ $ * + / | [] () - \ Because these characters are part of the regular expression, and therefore represents the split method, they need to use the escape character.

String str = "abc.def.ghi.jk";
String str1[] = str.split("\\.");
for(int i = 0; i < str1.length; i++)
	System.out.print(str1[i] + " ");

Operating results as follows:

abc def ghi jk 

Common String Methods

int length () Returns the current length of the string
int indexOf (String str) returns the position of the first occurrence of the string str sub in the string
int lastIndexOf (String str) str find the location of the sub-string of the last occurrence of
boolean comparison of the character string specified object equals (Object obj), returns true or to false
string TRIM () returns a string of spaces before and after extraction
returns a string [] split (string str) divides the character string in accordance str, after the division string array
string toLowerCase () string uppercase all lowercase
string toUpperCase () in the lower case string to all uppercase

Some examples:

  1. Java determines whether the input file name is correct, to determine whether the correct mailbox format. Where: legal file name should end with .java; legitimate mailbox name must contain at least @ and @ before asking.
public void judge(String fileName, String email){
	if(fileName.lastIndexOf(".java") == fileName.length() - 5 && fileName.length() != 5)
		System.out.println("Java文件名正确");
	else
		System.out.println("Java文件名无效");
	if(email.indexOf(".") > email.lastIndexOf("@") && email.lastIndexOf("@") != -1)
		System.out.println("邮箱名正确");
	else
		System.out.println("邮箱名无效");
}
  1. Find all position name appears in the data string
public class NameSearch {
    public static void main(String[] args) { 
        Scanner scanner = new Scanner(System.in);
		String data = scanner.next();
		String name = scanner.next();
		int index = 0;
		while(data.indexOf(name, index) != -1) {
			index = data.indexOf(name, index);
			System.out.println(index);
			index += 1;			
		}
    }
}
  1. Standard input a URL, the domain name of the output body; the body is converted to uppercase, printout.
import java.util.Scanner;
public class StringTest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		int index = str.indexOf(".");
		index ++;
		int index2 = str.indexOf(".", index);
		String str1 = str.substring(index, index2); //定位域名主体位于两个"."之间
		System.out.println(str1);
		String str2 = str1.toUpperCase();//小写转换成大写
		System.out.println(str2);
	}
}
Published 44 original articles · won praise 0 · Views 823

Guess you like

Origin blog.csdn.net/Komatsu_1137/article/details/104089166