String类方法集合(charAt、contains、endWith、IndexOf、lastIndexOf、length、replace、split、toCharArray、trim等等)

方法目录



/**
 * @author Ziph
 * @date 2020年3月2日
 * @Email mylifes1110@163.com
 * 
 * 根据下标获取字符串字符
 * 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 mylifes1110@163.com
* 
* 判断当前字符串中是否包含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 mylifes1110@163.com
* 
* 将字符串转换成数组
* 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 mylifes1110@163.com
* 
* 查找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 mylifes1110@163.com
 * 
 * 查找字符串在当前字符串中最后一次出现的下标索引 
 * 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 mylifes1110@163.com
* 
* 返回字符串的长度
* 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 mylifes1110@163.com
* 
* 去掉字符串前后的空格
* 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 mylifes1110@163.com
* 
* 将小写转成大写
* 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 mylifes1110@163.com
* 
* 判断字符串是否已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 mylifes1110@163.com
* 
* 将旧字符串替换成新字符串
* 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));
	}
}
  • 根据str做拆分
    • public String[] split(String str)
    • public String[] split(String regex, int limit)
    • 注意: . 、 $、 | 和 * 等转义字符,必须得加 \(转义字符),多个分隔符,可以用 | 作为连字符
/** 
* @author Ziph
* @date 2020年3月2日
* @Email mylifes1110@163.com
* 
* 根据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 mylifes1110@163.com
* 
* 截取字符串的某部分
* public String substring(int beginIndex, int endIndex)
* 该子字符串从指定的 beginIndex 处开始, endIndex:到指定的 endIndex-1处结束
*/
public class TestStringSubString {
	public static void main(String[] args) {
		//截取字符串中的mylifes1110
		String s = "mylifes1110@163.com";
		
		String s1 = s.substring(0, s.indexOf('@'));
		
		System.out.println(s1);	//输出mylifes1110
	}
}

/** 
* @author Ziph
* @date 2020年3月3日
* @Email mylifes1110@163.com
*/
public class TestIsEmail {
	public static void main(String[] args) {
		String s = "mylifes1110@163.com";
		
		//判断邮箱必须包含'@'和'.'
		//判断最后一个'.'的位置必须大于'@'的位置
		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 mylifes1110@163.com
*/
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 mylifes1110@163.com
*/
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);
	}
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java String 类型 API 测试代码 1.String和char[]之间的转换 toCharArray(); 2.String和byte[]之间的转换 getBytes() Arrays工具类 : Arrays.toString(names) StringString replace(char oldChar, char newChar) String replace(CharSequence target, CharSequence replacement) String[] split(String regex) boolean contains(CharSequence s):当且仅当此字符串包含指定的 char 值序列时,返回 true int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引 int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始 int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引 int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索 boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束 boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始 boolean startsWith(String prefix, int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始 int length():返回字符串的长度: return value.length char charAt(int index): 返回某索引处的字符return value[index] boolean isEmpty():判断是否是空字符串:return value.length == 0 String toLowerCase():使用默认语言环境,将 String 中的所有字符转换为小写 String toUpperCase():使用默认语言环境,将 String 中的所有字符转换为大写 String trim():返回字符串的副本,忽略前导空白和尾部空白 boolean equals(Object obj):比较字符串的内容是否相同 boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写 String concat(String str):将指定字符串连接到此字符串的结尾。 等价于用“+” String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。 String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值