JAVA第七周学习总结
String类
String类的常用方法
方法 | 目的 | 举例 | 运行&结果 | 注意 |
---|---|---|---|---|
length() | 用来获取一个String对象的字符序列的长度 | String china = "1945抗战胜利";int n1,n2;n1 = china.length();n2 = "小鸟fly”.length(); |
n1的值为9,n2的值为5 | |
equals(String s) | 比较当前String对象的字符序列是否参与参数s指定的String对象字符序列相同 | String tom = new String("天道酬勤"); String boy = new String("知心朋友");String jerry = new String("天道酬勤"); |
tom.equals(boy)的值为false,tom.equals(jerry)的值为ture,tom = jerry 的值为false | 比较时忽略大小写 |
starts With(String s) | 判断当前String对象的字符序列前缀是否是参数指定的String对象s的字符序列 | String tom = "天气预报,阴有小雨",jerry = "比赛结果,中国队赢得胜利"; |
tom.statsWith("天气")的值为true,jerry.statsWith("天气")的值为false | endsWith(String s)方法恰与其相反,判断后缀 |
compareTo(String s) | 按字典序与参数指定的String对象s的字符序列比较大小,与s相同返回值0,大于s返回正值,小于s返回负值(a在Unicode中排序为97) | String str = "abcde" |
str.compareTo("boy")小于0,str.compareTo("aba")大于0,str.compareTo("abcde")等于0 | public int compareToIgnoreCase(String s)方法用法相同,此方法忽略大小写 |
contains (String s) | 判断当前String对象的字符序列是否包含参数s的字符序列 | tom = "student" |
tom.contains("stu")的值为ture,tom.contains("ok")的值为false |
码云链接及脚本截图
unit8
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
更多精彩