码迷,mamicode.com
首页 > 编程语言 > 详细

Java中String的常用方法总结

时间:2019-03-23 22:38:25      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:字典顺序   返回   tar   substr   字符   getchar   nbsp   特定   print   

Java中String的常用方法总结

 

1、length()字符串的长度

1  String str="HelloWord";
2  System.out.println(str.length());

输出结果是10

2、charAt() 截取一个字符

 

3 getchars()截取多个字符并由其他字符串接收

 

4 getBytes()将字符串变成一个byte数组

 

5 toCharArray()将字符串变成一个字符数组

 

6 equals()和equalsIgnoreCase()比较两个字符串是否相等,前者区分大小写,后者不区分

 

 7 startsWith()和endsWith()判断字符串是不是以特定的字符开头或结束

 

8 toUpperCase()和toLowerCase()将字符串转换为大写或小写

 

9 concat() 连接两个字符串

 

 

10 trim()去掉起始和结束的空格

 

11 substring()截取字符串

 

12 indexOf()和lastIndexOf()前者是查找字符或字符串第一次出现的地方,后者是查找字符或字符串最后一次出现的地方

 

13 compareTo()和compareToIgnoreCase()按字典顺序比较两个字符串的大小,前者区分大小写,后者不区分

 

14 replace() 替换

 

代码演示:

 1 package com.aaa.demo9;
 2 
 3 public class StringDemo {
 4     public static void main(String[] args) {
 5         
 6         // 2、charAr() 截取一个字符
 7         String charAtDemo="hello";
 8         System.out.println(charAtDemo.charAt(2));
 9         //输出的是l
10         
11         //3 getchars()截取多个字符并由其他字符串接收
12         String getChars="hello";
13         char[] getCharDemo=new char[10];
14         getChars.getChars(0, 3, getCharDemo, 0);
15         //第一个数值代表的是截取的字符串开始的位置
16         //第二个数值代表截取要截取的字符串的结束后的下一个下标(也可以理解为截取的长度)
17         //第三个代表的是接收的字符串数组,最后一个参数是接收数组的开始位置。
18         System.out.println(getCharDemo);
19         
20         //4 getBytes()将字符串变成一个byte数组
21         String getByte="hello";
22         byte[] b=getByte.getBytes();
23         System.out.println(new String(b));
24         
25         
26         //5 toCharArray()将字符串变成一个字符数组
27         String charArr="hello";
28         char[] c = charArr.toCharArray();
29         System.out.println(c);
30         
31         //6 equals()和equalsIgnoreCase()比较两个字符串是否相等,前者区分大小写,后者不区分
32         String equalA="hello";
33         String equalB="hello";
34         boolean equals = equalA.equals(equalB);
35         System.out.println(equals);
36         //结果为true
37         
38         //7 startsWith()和endsWith()判断字符串是不是以特定的字符开头或结束
39         String startA="hello.java";
40         boolean endsWith = startA.endsWith(".java");
41         System.out.println(endsWith);
42         //结果为true
43         
44         //8 toUpperCase()和toLowerCase()将字符串转换为大写或小写
45         String upperCase="Hllo";
46         String upp = upperCase.toUpperCase();
47         System.out.println(upp);
48         //输出结果为HLLO
49         
50         //9 concat() 连接两个字符串
51         String concatA="hello";
52         String concatB="你好";
53         String concat = concatA.concat(concatB);
54         System.out.println(concat);
55         
56         //10 trim()去掉起始和结束的空格
57         String trimA="    hello    ";
58         System.out.println(trimA.trim());
59         
60         //11 substring()截取字符串
61         String subA="hello";
62         System.out.println(subA.substring(1));
63         
64         //12 indexOf()和lastIndexOf()前者是查找字符或字符串第一次出现的地方,后者是查找字符或字符串最后一次出现的地方
65         String indexA="hello";
66         System.out.println("index"+indexA.indexOf("e"));//返回的是下标的值,如果找不到返回-1
67         
68         //13 compareTo()和compareToIgnoreCase()按字典顺序比较两个字符串的大小,前者区分大小写,后者不区分
69         String comA="hello";
70         String comB="Word";
71         System.out.println(comA.compareTo(comB));
72         
73         //14 replace() 替换
74         String reA="hello";
75         reA=reA.replace("e", "a");
76         System.out.println(reA);
77         
78     }
79 }

 

Java中String的常用方法总结

标签:字典顺序   返回   tar   substr   字符   getchar   nbsp   特定   print   

原文地址:https://www.cnblogs.com/yanpingping/p/10585676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!