String class set of methods (charAt, contains, endWith, IndexOf, lastIndexOf, length, replace, split, toCharArray, trim, etc.)

Hello everyone, I'm Ziph!

This is my summary of the collection of the String class methods! OK!

In order to facilitate everyone to find, click on the directory title to jump the appropriate method code examples. Click on the title to return to the code sample catalog!

table of Contents:

/**
 * @author Ziph
 * @date 2020年3月2日
 * @Email [email protected]
 * 
 * 根据下标获取字符串字符
 * public char charAt(int index)
 */
public class TestStringCharAt {
	public static void main(String[] args) {
		String s = "123456";
		
		System.out.println(s.charAt(0));//访问字符串的第一个字符
		//这里我随便用了个抛出异常,大家可以忽略
		try {
			System.out.println(s.charAt(6));//下标越界
		} catch (Exception e) {
			System.out.println("下标越界!");//抛出异常
		}
		
		for (int i = 0; i < s.length(); i++) {//将字符串中的字符逐个按行输出
			System.out.print(s.charAt(i));
		}
	}
}
/** 
* @author Ziph
* @date 2020年3月2日
* @Email [email protected]
* 
* 判断当前字符串中是否包含str
* public boolean contains(String str)
*/
public class TestStringContains {
	public static void main(String[] args) {
		String s = "123456";
		
		//判断当前字符串中是否包含2
		System.out.println(s.contains("2"));	//true
		
		//判断当前字符串中是否包含0
		System.out.println(s.contains("0"));	//false
	}
}
/** 
* @author Ziph
* @date 2020年3月2日
* @Email [email protected]
* 
* 将字符串转换成数组
* public char[] toCharArray()
*/
public class TestStringToCharArray {
	public static void main(String[] args) {
		//ToCharArray()将字符串对象中的字符转换为一个字符数组。
		//字符串转换成字符数组后,每个字符的ASC码与字符T的ASC码进行二进制异或运算。
		//最后把结果转换回字符。
		String s = "www.ziph.cn";
		
		System.out.println(s.toCharArray());//普通打印和转换后打印一致
		
		char[] a = s.toCharArray();//用数组接收转换后的数组
		
		for (int i = 0; i < a.length; i++) {//打印接收后的数组,每一个字符后打印一个空格显示输出内容
			System.out.print(a[i] + " ");	//数组下标代表所对应的字符串字符
		}
	}
}
/** 
* @author Ziph
* @date 2020年3月2日
* @Email [email protected]
* 
* 查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1
* public int indexOf(String str)
* 从fromIndex的下标位置开始查找字符串中ch字符的首次出现下标
* public int IndexOf(String str, int fromIndex) 
*/
public class TestStringIndexOf {
	public static void main(String[] args) {
		String s = "896988359";
		
		//查找8的首次出现下标
		System.out.println(s.indexOf("8"));	 	//输出下标	0
		
		//查找a字符首次出现下标
		System.out.println(s.indexOf("a")); 	//找不到,输出-1
		
		//从0下标位置开始找首个8
		System.out.println(s.indexOf("8", 0));	//输出下标:	0
		
		//从2下标位置开始找首个8
		System.out.println(s.indexOf("8", 2));	//输出下标:	4
		
		//从7下标位置开始找首个8
		System.out.println(s.indexOf("8", 7));	//找不到,输出-1
	}
}
/**
 * @author Ziph
 * @date 2020年3月2日
 * @Email [email protected]
 * 
 * 查找字符串在当前字符串中最后一次出现的下标索引 
 * public int lastIndexOf(String str)
 * 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索,如果此字符串中没有这样的字符,则返回 -1
 * public int lastIndexOf(String str, int fromIndex) 
 */
public class TestStringLastIndexOf {
	public static void main(String[] args) {
		String s = "896988359";

		// 查找8的最后一次出现下标
		System.out.println(s.lastIndexOf("8")); // 输出下标: 5

		// 查找a字符最后一次出现下标
		System.out.println(s.lastIndexOf("a")); // 找不到,输出-1

		// 从0下标位置开始反向找最后一个88
		System.out.println(s.lastIndexOf("88", 0)); //反向没有找到,输出-1

		// 从6下标位置开始反向找最后一个88
		System.out.println(s.lastIndexOf("88", 6)); // 输出下标: 4
	}
}
/** 
* @author Ziph
* @date 2020年3月2日
* @Email [email protected]
* 
* 返回字符串的长度
* public int length()
*/
public class TestStringLength {
	public static void main(String[] args) {
		String s = "Ziph";
		
		System.out.println(s.length());//返回字符串长度,输出4
		
	}
}
/** 
* @author Ziph
* @date 2020年3月2日
* @Email [email protected]
* 
* 去掉字符串前后的空格
* public String trim()
*/
public class TestStringTrim {
	public static void main(String[] args) {
		String s = "     Hello!";
		
		//打印带空格
		System.out.println(s);//输出     Hello!
		
		//打印把字符串前面的空格去掉了
		System.out.println(s.trim());//输出Hello!
	}
}
/** 
* @author Ziph
* @date 2020年3月2日
* @Email [email protected]
* 
* 将小写转成大写
* public String toUpperCase()
*/
public class TestStringUpperCase {
	public static void main(String[] args) {
		String s = "Ziph";
		
		//将小写转化为大写
		System.out.println(s.toUpperCase());//输出ZIPH
	}
}
/** 
* @author Ziph
* @date 2020年3月2日
* @Email [email protected]
* 
* 判断字符串是否已str结尾
* public boolean endWith(String str)
*/
public class TestStringEndWith {
	public static void main(String[] args) {
		String s = "HelloWorld!";
		
		//判断字符串是否以“H”字符串结尾
		System.out.println(s.endsWith("H"));	//输出false
		
		//判断字符串是否以“!”字符串结尾
		System.out.println(s.endsWith("!"));	//输出true
	}
}
/** 
* @author Ziph
* @date 2020年3月2日
* @Email [email protected]
* 
* 将旧字符串替换成新字符串
* public String replace(char oldChar, char newChar)
*/
public class TestStringReplace {
	public static void main(String[] args) {
		String s1 = "Tom";
		String s2 = "Ziph";
		
		//将s1字符串替换成新字符串s2
		System.out.println(s1.replace(s1, s2));
	}
}
  • According to split do str
    • public String[] split(String str)
    • public String[] split(String regex, int limit)
    • Note :., $, | And escape characters such as *, have to add \ (escape character), more than one delimiter can be used | as a hyphen
/** 
* @author Ziph
* @date 2020年3月2日
* @Email [email protected]
* 
* 根据str做拆分
* public String[] split(String str)
* 根据str做拆分,regex表示正则表达式分隔符,limit表示分割的份数。
* public String[] split(String regex, int limit)
* 注意: . 、 $、 | 和 * 等转义字符,必须得加 \\
* 注意:多个分隔符,可以用 | 作为连字符
*/
public class TestStringSplit {
	public static void main(String[] args) {
		//根据“-”拆分
        String s1 = "Welcome-to-Beijing";
        String[] ss1 = s1.split("-"); 
        for (int i = 0; i < ss1.length; i++) {
        	System.out.print(ss1[i] + " ");//输出:Welcome to Beijing 
		}
        
        System.out.println();
        
        //根据“-”拆分成2份
        String[] ss2 = s1.split("-", 2);
        for (int i = 0; i < ss2.length; i++) {
        	System.out.print(ss2[i] + " ");//输出:Welcome to-Beijing 
		}
        
        
        System.out.println();
        
        //根据.拆分
        String s2 = "192.168.1.1";
        // . 必须得加 转义字符\\
        for (String s3 : s2.split("\\.")){
            System.out.print(s3 + " ");//输出:192 168 1 1
        }
	}
}
/** 
* @author Ziph
* @date 2020年3月3日
* @Email [email protected]
* 
* 截取字符串的某部分
* public String substring(int beginIndex, int endIndex)
* 该子字符串从指定的 beginIndex 处开始, endIndex:到指定的 endIndex-1处结束
*/
public class TestStringSubString {
	public static void main(String[] args) {
		//截取字符串中的mylifes1110
		String s = "[email protected]";
		
		String s1 = s.substring(0, s.indexOf('@'));
		
		System.out.println(s1);	//输出mylifes1110
	}
}

/** 
* @author Ziph
* @date 2020年3月3日
* @Email [email protected]
*/
public class TestIsEmail {
	public static void main(String[] args) {
		String s = "[email protected]";
		
		//判断邮箱必须包含'@'和'.'
		//判断最后一个'.'的位置必须大于'@'的位置
		if (s.indexOf('@') != -1 && s.indexOf('.') != -1) {
			if (s.indexOf('@') < s.lastIndexOf('.')) {
				System.out.println("合法邮箱!");
			} else {
				System.out.println("不合法邮箱!");
			}
		} else {
			System.out.println("不合法邮箱!");
		}
	}
}
/** 
* @author Ziph
* @date 2020年3月3日
* @Email [email protected]
*/
public class TestStringUUID {
	public static void main(String[] args) {
		//随机获取UUID码
		String s = java.util.UUID.randomUUID().toString();
		//打印查看码的格式
		System.out.println(s);//4a8c8dab-633f-4491-b7df-b2fe48a2a91e
		//去掉“-”
		String s1 = s.replace("-", "");
		//打印看结果
		System.out.println(s1);//4a8c8dab633f4491b7dfb2fe48a2a91e
	}
}
import java.util.Random;

/** 
* @author Ziph
* @date 2020年3月3日
* @Email [email protected]
*/
public class TestGetRandomNumber {
	public static void main(String[] args) {
		String s = "ABCDEFGhijklmn1234567";
		Random random = new Random();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < 4; i++) {
			int index = random.nextInt(s.length());
			char c = s.charAt(index);
			sb.append(c);
		}
		System.out.println(sb);
	}
}
Published 87 original articles · won praise 143 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_44170221/article/details/104618884