平台网站开发可行性分析,linux下网站搭建,黔西南州建设银行网站,wordpress 禁止访问String类中常用的类方法#xff1a;
方法名称描述format(String format, Object... args)使用指定的格式字符串和参数返回一个格式化字符串。 format - 格式字符串 args - 格式字符串中由格式说明符引用的参数。如果还有格式说明符以外的参数#xff0c;则忽略这些额外的参数…String类中常用的类方法
方法名称描述format(String format, Object... args)使用指定的格式字符串和参数返回一个格式化字符串。 format - 格式字符串 args - 格式字符串中由格式说明符引用的参数。如果还有格式说明符以外的参数则忽略这些额外的参数。参数的数目是可变的可以为 0。参数的最大数目受 Java Virtual Machine Specification 所定义的 Java 数组最大维度的限制。有关 null 参数的行为依赖于转换。valueOf(Object obj)返回 Object 参数的字符串表示形式。valueOf(char[] data)返回 char 数组参数的字符串表示形式。字符数组的内容已被复制后续修改不会影响新创建的字符串。valueOf(char[] data, int offset, int count)返回 char 数组参数的特定子数组的字符串表示形式。 offset 参数是子数组的第一个字符的索引。count 参数指定子数组的长度。字符数组的内容已被复制后续修改不会影响新创建的字符 data - 字符数组。 offset - String 值的初始偏移量。 count - String 值的长度。copyValueOf(char[] data, int offset, int count)返回 char 数组参数的特定子数组的字符串表示形式。 data - 字符数组。 offset - String 值的初始偏移量。 count - String 值的长度。copyValueOf(char[] data)返回指定数组中表示该字符序列的 String。 data - 字符数组。valueOf(boolean b)返回 boolean 参数的字符串表示形式。valueOf(char c)返回 char 参数的字符串表示形式。valueOf(int i)返回 int 参数的字符串表示形式。valueOf(long l)返回 long 参数的字符串表示形式。valueOf(float f)返回 float 参数的字符串表示形式。valueOf(double d)返回 double 参数的字符串表示形式。join(String s,str1,str2....)以s为连接符连接字符串
public class Customer {
Overridepublic String toString() {return 我是重写的toString方法;}
}
public class StringTest4{ public static void main(String[] args){/** 静态方法* String中只有一个方法是静态的不需要new对象* 将非字符串转换为字符串* */double d1 0.023;System.out.println(String.format(%.2f, d1));char [] chars {x,y,z};System.out.println(String.copyValueOf(chars));//xyzSystem.out.println(String.copyValueOf(chars,1,2));//yzSystem.out.println(类方法);//String s String.valueOf(true);//把boolean类型转换为字符串//String s String.valueOf(3.14);//把浮点类型转换为字符串//System.out.println(s);String s String.valueOf(new Customer());//System.out.println(s);//没有重写toString方法之前是对象的内存地址test.Customer7de26db8System.out.println(s);//System.out.println(String.join(., www,baidu.com));//www.baidu.com}
} String类中常用的实例方法
方法名称描述equals(String string)判断字符串是否相等equalslgnoreCase(String string)忽略大小写判断是否相等length()获取字符串的长度charAt(int index)获取某个索引处的字符charindexOf(String string)返回指定子字符串在此字符串中第一次出现处的索引。返回int类型的索引找不到返回-1indexOf(String stringint startIndex)返回指定子字符串在此字符串中第一次出现处的索引从指定的索引startIdex开始。lastindexOf(String string)返回指定子字符串在此字符串中最后一次出现处的索引。lastindexOf(String str, int endsIndex)返回指定子字符串在此字符串中最后一次出现处的索引从指定的索引endsIndex开始反向搜索。startsWith(String string)判断是否以string开始endsWith(String string)判断是否以string结束comparTo(String string)比较字符串的大小toLowerCase()将字符串转化为小写toUpperCase()将字符串转换为大写subString(int index)从index位置到截取到字符串的末尾subString(int startindex,int endsindex)从开始索引位置开始到结束索引位置结束前闭后开区间trim()去除字符串前后的空格replace(char oldChar, char newChar)返回一个新的字符串它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。spilt(String string)根据给定正则表达式的匹配拆分此字符串。split(String string,int limit)分割保留末尾的空字符concat(String string)将指定字符串连接到此字符串的结尾。contains(String string)判断是否含stringtoCharArray将字符串转换为字符数组intern()返回字符串对象的规范化表示形式。/br一个初始为空的字符串池它由类 String 私有地维护。 当调用 intern 方法时如果池已经包含一个等于此 String 对象的字符串用 equals(Object) 方法确定则返回池中的字符串。否则将此 String 对象添加到池中并返回此 String 对象的引用。 它遵循以下规则对于任意两个字符串 s 和 t当且仅当 s.equals(t) 为 true 时s.intern() t.intern() 才为 true。isEmpty()判断字符串是否为空
public class StringTest5{public static void main(String[] args){String s1 new String(http://WWW.baidu.com); String s2baidu;//boolean equals(String string) 判断两个字符串是否相等 地址 长度 每个字符 equals只能判断是否相等而compareTo除了看出是否相等还等看出大小System.out.println(判断两个字符串是否相等);String s new String(xyz);System.out.println(xyz.equals(s));//true//boolean equalsIgnoreCase(String string)System.out.println(忽略大小写后判断两个字符串是否相等);System.out.println(XyZ.equalsIgnoreCase(s));//true//int length()System.out.println(获取字符串的长度);System.out.println(s2.length());//5//char charAt(int index) 获取字符串对应索引的字符System.out.println(获取字符串对应索引的字符);char c s2.charAt(1);System.out.println(c);//好//a//indexOf(String string) 判断某个子字符串在字符串上第一次出现处的索引System.out.println(判断某个子字符串在字符串上第一次出现处的索引);System.out.println(s1.indexOf(.)); //10//indexOf(String stringint startIndex)判断某个子字符串在字符串上从指定索引startindex开始第一次出现处的索引System.out.println(判断某个子字符串在字符串上从指定索引startindex开始第一次出现处的索引);System.out.println(s1.indexOf(., 11));//16//lastindexOf(String string) 返回指定子字符串在此字符串中最后一次出现处的索引。System.out.println(返回指定子字符串在此字符串中最后一次出现处的索引。);System.out.println(s1.lastIndexOf(.)); //16//lastindexOf(String str, int endsIndex) 返回指定子字符串在此字符串中最后一次出现处的索引从指定的索引endsIndex开始反向搜索。System.out.println(返回指定子字符串在此字符串中最后一次出现处的索引从指定的索引endsIndex开始反向搜索。);System.out.println(s1.lastIndexOf(.,15)); //10//boolean contains(String string) 判断前面的字符串是否包含后面的字符串System.out.println(判断前面的字符串是否包含后面的字符串);System.out.println(helloworld.contains(world));//trueSystem.out.println(s.contains(https://));//false//boolean startsWith(String string) 判断当前字符串是否以某个字符串开始System.out.println(判断当前字符串是否以某个字符串开始);System.out.println(s1.startsWith(https://));//false//boolean endsWith(String string) 判断当前字符串是否以某个字符串结尾System.out.println(判断当前字符串是否以某个字符串结尾);System.out.println(test.txt.endsWith(.java));//falseSystem.out.println(test.txt.endsWith(.txt));//true//int comparTo(String string) System.out.println(按照字典顺序比较两个字符串大小);int res1 abc.compareTo(abc);System.out.println(res1);//0 前后一致 10-10 0int res2 abcd.compareTo(abcde);System.out.println(res2);//-1 前小后大 9-10 -1int res3 abce.compareTo(abcd);System.out.println(res3);//1 前大后小 10-9 1int res4 abc.compareTo(bac);System.out.println(res4);//-1 两个字符串对应位置的字符依此按照字典顺序比较分出胜负就不比较了//toLower()System.out.println(将字符串转换为小写);System.out.println(s1.toLowerCase());//http://www.baidu.com//toUpper()System.out.println(将字符串转换为大写);System.out.println(s1.toUpperCase());//HTTP://WWW.BAIDU.COM//string subString(int index)System.out.println(将字符串从索引index位置截取到结尾);//WWW.baidu.comSystem.out.println(s1.substring(7));//string subString(int index)System.out.println(将字符串从索引startsindex位置截取到索引endsindex位置,前闭后开区间);System.out.println(s1.substring(7,10));//WWW//trim()System.out.println(去除字符串前后的空格);System.out.println( xyz .trim());//xyz//replace(String string)System.out.println(返回一个新的字符串它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的);System.out.println(s1.replace(http://,https://));//https://WWW.baidu.com//split(String string)System.out.println(按照正则表达式string分割字符);String [] time 2022-5-22.split(-);for(int i0;itime.length;i) {System.out.println(time[i]);}/** 2022522* *///split(String string,int limit)System.out.println(分割保留末尾的空字符);String [] time1 2022-5-22 .split(-,3);for(int i0;itime1.length;i) {System.out.println(time1[i]);}/** 2022522 * *///join(String s,str1,str2....)System.out.println(以s为连接符连接字符串);System.out.println(String.join(., www,baidu.com));//www.baidu.com//concat(String string) 将指定字符串连接到此字符串的结尾。System.out.println(将指定字符串连接到此字符串的结尾。);System.out.println(s2.concat(.com));//baidu.com//char[] toCharArray 将字符串转换为字符数组System.out.println(将字符串转换为字符数组);char [] charss2.toCharArray();for(int i 0;ichars.length;i) {System.out.println(chars[i]);}/** baidu* *///intern() 返回字符串对象的规范化表示形式。System.out.println(返回字符串对象的规范化表示形式。);System.out.println(s1.intern());//http://WWW.baidu.com//isEmpty() 判断某个字符串是否为空字符串 数组长度是length属性字符串长度是length方法System.out.println(判断某个字符串是否为空字符串);System.out.println(s2.isEmpty());//false }
}