12-String common methods

Hello everyone, I am a pig arched by cabbage.

1. Get the inherent property method of String

(1) Length: length (), pay attention to the difference with the array, in the array, length is a property of it, here is a method of String.
(2) Start with what string startsWith ()
(3) End with what string endsWith ()
can also convert the string into a character array, using toCharArray () method

String info = "abc123";
		int len = info.length();
		System.out.println(len);
		
		//char[] array = info.toCharArray();
		
		String info2 = "http://www.baidu.com";
		if (info2.startsWith("http")) {
			System.out.println("用浏览器打开");
		} else {
			System.out.println("用文件系统打开");
		}
		
		boolean b = info2.endsWith("com");
		if (b) {
			System.out.println("商业运营公司");
		} else if (info2.endsWith("edu")) {
			
		} else if (info2.endsWith("org")) {
			
		}

2. Two string comparison methods

(1) == When judging a string, it is compared whether the address of the two strings is the same.
For basic types, like int, float compares whether the value is the same.
For the string, it is the address.

To determine whether the contents of two strings are equal, use the following two methods
(2) equals, to determine whether the contents of two strings are the same

String s1 = "abc";
		String x = "ab";
		String y = "x";
		String s2 = x + y;
		
		System.out.println(s1);
		System.out.println(s2);
		// == 判断字符串的时候,判定的是两个字符串的地址是否一致
		System.out.println(s1 == s2);
		// equals 判定的是字符串的内容是否一致
		System.out.println(s1.equals(s2));

(3) compareTo ()
A and B do compare
A. compareTo (B)
means AB makes a difference.
If the first char of A and B is inconsistent, the first difference is the overall difference between the two strings.
If A and B The first char is the same, look at the second char, the rule == first
If one of the strings has reached the end, the difference between the length of the two strings is the overall difference between the two strings

int sub = s1.compareTo(s2);

3.String search attribute

Find the keyword
(1) indexOf ("what to find", where to start)
indexOf ("what to find") start from 0 without writing in the brackets
(2) lastIndexOf ("what to find") The last position of
lastlastOf "Find what", as of where) the
first character position of the string is 0, instead of starting from 1
(3) truncating the information around the keyword to
subString (start, end) [start, end) half closed, half open, Does not contain the last character that is pointed to by end
substring (start) start to the end

String str = "abcd汉武帝efgh";
		
		/*int p = str.indexOf("汉武帝", 1);
		System.out.println(p);*/
		
		int lp = str.lastIndexOf("汉武帝", 7);
		System.out.println(lp);
		
		//String 子串 = str.substring(lp - 2, lp + 2+ "汉武帝".length());
		String 子串 = str.substring(1, 7);	
		System.out.println(子串);

4. String split and replace

(1) split () splits a string into a string array
(2) replaceAll () replaces all

String info = "20190727-20-220201199801010123-16-20190301-张三";
		
		String[] infos = info.split("-");
		
		for (int i = 0; i < infos.length; i++) {
			System.out.println(infos[i]);
		}
		
		String r = info.replaceAll("2", "贰").replaceAll("1", "壹");
		System.out.println(r);
	}
Published 24 original articles · praised 4 · visits 2038

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/97532364