String类中常用的一些个方法 拼搏现实的明天。 2022-02-17 00:48 179阅读 0赞 代码测试: package com.lzw.demo; /** * @author lzw * @Date 2019年4月11日 */ public class Dmoe { private static String str = "abcde12d3456"; private static String str2 = " 123456abc "; public static void main(String[] args) { System.out.println("equals()---:"+str.equals(str2));//输出true或者false 比较两个字符串 System.out.println("length()---:"+str.length());//输出字符串的长度 System.out.println("indexOf()---:"+str.indexOf("d"));//输出指定字符第一次在字符串中的位置 System.out.println("charAt()---:"+str.charAt(3));//输出指定位置的值;起始为0 System.out.println("replace()---:"+str.replace("12", "ggg"));//字符串替换 System.out.println("trim()---:"+str2.trim());//去除字符串首尾的空格 System.out.println("substring()---:"+str.substring(4)+":"+str.substring(2, 6));//截取字符串 } } 输出结果: equals()---:false length()---:12 indexOf()---:3 charAt()---:d replace()---:abcdegggd3456 trim()---:123456abc substring()---:e12d3456:cde1
还没有评论,来说两句吧...