String 常用方法

  1.String.contains("www");查询字符串中是否存在“www”, 返回值是布尔值

1 public class Test{
2     public static void main(String[] args){
3         String path = "http://www.baidu.cn";
4         System.out.println(path.contains("www"));//true//不存在则返回false
5 } 6 }

  2.String.endsWith("cn");查询字符串是否以“cn”结尾,返回值为布尔值

1 System.out.println(path.endsWith("com"));//true
2 System.out.println(path.endsWith("www"));//false

 

  3.String.startsWith("www");查询字符串是否以“www”开头,返回值为布尔值

1 System.out.println(path.startsWith("www"));//false
2 System.out.println(path.startsWith("http"));//true

 

  4.String.length();输出字符串的长度

1 //输出字符串长度
2 System.out.println(path.length());//20

 

  5.String.charAt(5);返回字符串索引值为5的元素

1 System.out.println(path.charAt(0));//h
2 //System.out.println(path.charAt(30));//如果索引值超出字符串的长度,则抛出异常StringIndexOutOfBoundsException

 

  6.String.indexof("w");返回第一个“w”出现的索引值,如果不存在则返回“-1”

  String.indexof("w", 88); 从索引为88的元素开始查起,返回第一个遇到的“w”的索引值,如果88大于字符串长度也返回“-1”

1 //查找元素在字符串中的下标
2 System.out.println(path.indexOf("w"));//7 //若有多个,则返回第一个的下标
3 System.out.println(path.indexOf("l"));//不存在则返回-1
4 System.out.println(path.indexOf("w",8));//8
5 
6 System.out.println(path.indexOf("w",99));//超出返回-1

  7.String.concat("world");在字符串末尾追加“world”

1 String str = "hello world";
2 //追加
3 String strs = str.concat(" lalala");
4 System.out.println(str);//hello world//追加并没有改变原本的字符串,String是不可改变的,因为底层是用fianl修饰的char[]数组来实现的
5 System.out.println(strs);//hello world lalala

 

  8.String.substring(1);从下标为1的位置开始截取,直到字符串结束

  String.substring(1,4);从下标为1的位置开始截取,下标为4的位置结束,不包含下标为4的元素(左闭右开区间)

1         //截断,从第一个数字的下标开始
2         System.out.println(str.substring(1));//ello world
3         //从下标为1开始,下标为4结束,不包含4,[1,4)
4         System.out.println(str.substring(1,4));//ell

  9.String.trim();截取掉,字符串前后的空格,而不包含字符串当中的

1         //去掉前后空格,中间的不能去掉
2         String str1 = "   hello everyone!   ";
3         System.out.println(str1.trim());//hello everyone!        

  10.String.toUpperCase();将字符串变为大写的

1         //转大写
2         System.out.println(str.toUpperCase());//HELLO WORLD    

 

  11.String.toLowerCase();将字符串变为小写的

1          //转小写
2          String str2 = "HELLO WORLD";
3          System.out.println(str2.toLowerCase());//hello world

  12.String.getBytes();将字符串转化为 byte 类型的数组

1         //把字符串转化为byte类型数组
2         byte[] bytes = str.getBytes();
3         //输出时给出的是每个字符对应的ASC码
4         System.out.println(Arrays.toString(bytes));//[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]    

 

  13.String.toCharArray();将字符串转化为char类型的数组

1         char[] chars = str.toCharArray();
2         System.out.println(Arrays.toString(chars));//[h, e, l, l, o,  , w, o, r, l, d]

  14.String.split(l); 以字符串中的元素“l”,对字符串进行分割, String.split(l, n),则是将字符串以元素“l”分割成 n 段。若n大于元素“l”的个数或者等于0,则与前者无异

1         String[] arr = str.split("l");
2         System.out.println(Arrays.toString(arr));//[he, , o wor, d]

 

  15.String.equals(String1);比较两个字符串的值是否相同,str1== str2则对比的是两个变量存储的地址是否相同

1         String str1 = "HELLO";
2         String str2 = "hello";
3 
4         System.out.println(str1.equals(str2));//false
5         //不区分大小写比较
6         System.out.println(str1.equalsIgnoreCase(str2));//true

 

  16.String.isEmpty();则是判断字符串是否为空

1 String str1 = "HELLO";
2 String str3 = "";
3 System.out.println(str3.isEmpty());//true
4 System.out.println(str1.isEmpty());//false、

  17.String.replace(oldstr, newstr); 返回一个新的字符串,其中oldstr被newstr替换。

1 String str  = "XXX今天真好运!";
2 String str1 = str.replace("XXX", "王五");
3 System.out.println(str);//XXX今天真好运!
4 System.out.println(str1);//王五今天真走运

 

posted @   yuedongfan  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示