Java get string information

As an object, a character string can be used to obtain valid information about the character string, such as obtaining the length of a character string and the character at a certain index position.
1. Get
the substring The string can be intercepted by the substring() method of the String class. The common point of these methods is to use the subscript of the string to intercept, and it should be clear that the subscript of the string starts from 0. The substring() method is overloaded by two different methods to meet different needs.
(1) substring(int beginIndex)
This method returns the string that is intercepted from the specified index position until the end of the string. Spaces in the string will also occupy an index position. Sample code:

public class StringOne {
	public static void main(String[] args) {
		String str="Hello Word";
		String substr=str.substring(3);
		//字符串str对应的索引
		//0 1 2 3 4 5 6 7 8 9
		//H e l l o   W o r d
		System.out.println(substr);//输出结果:lo Word
	}
}

(2) substring (int beginIndex, int endIndex)
This method returns the substring that starts at a certain index position of the string and ends at a certain index position. beginIndex refers to the index position of the string to be intercepted, the position of the endIndex substring in the entire string, sample code:

String str1="My Practice";
//字符串str1对应的索引
//0 1 2 3 4 5 6 7 8 9 10
//M y   P r a c t i c e
String str2=str1.substring(0, 4);
System.out.println(str2);//输出结果:My P

2. Get the length of the string
Use the length() method of the String class to get the length of the declared string object. Sample code:

String str3="I need your help";
int inta=str3.length();
System.out.println(inta);//输出结果:16

3. String search The
String class contains two methods to search for strings, namely indexOf() and lastIndexOf() methods, both of which allow searching for characters or strings of specified conditions in the string.
(1) indexOf(String str)
This method is used to return the index position of the first occurrence of the parameter string str in the specified string. When using this method, the position of str will be searched from the beginning of the current string. If the string str is not retrieved, the return value of this method is -1. Sample code:

String str4="Somthing just like this";
int intb=str4.indexOf("t");
System.out.println(intb);//输出结果:3

(2) lastIndexOf(String str)
This method is used to return the index position of the last occurrence of the specified string. When using this method, search str from the beginning of the current string and return the index position of the last occurrence of str. If the string str is not retrieved, the method returns -1. If the parameter in the lastIndexOf() method is an empty string "" (no spaces), the returned result is the same as that of the length() method. Sample code:

String str5="Good luck";
int intc=str5.lastIndexOf("o");
System.out.println(intc);//输出结果:2
int intd=str5.lastIndexOf("");
System.out.println(intd);//输出结果:9
System.out.println(str5.length());//输出结果:9

4. Get the character at the specified index position.
Use the charAt() method to return the character at the specified index. Sample code:

String str6="Web Browser";
char char1=str6.charAt(5);
//将字符串str6中索引位置是5的字符返回
System.out.println("字符串str6中索引位置是5的字符为:"+char1);

Screenshot of running result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44547592/article/details/93758133