Java 常用类 String的常用方法(1)

 1 package com.bytezero.stringclass;
 2 
 3 import org.junit.Test;
 4 
 5 import java.sql.SQLOutput;
 6 import java.util.Locale;
 7 
 8 /**
 9  *
10  *              String 常用方法(1)
11  * int Length():  返回字符串的长度: return value.length
12  * char charAt(int index):返回某索引处的字符 return value[index]
13  * boolean isEmpty(): 判断是否是空字符串: return value.length == 0;
14  * String toLowerCase(): 使用默认语言环境,将String中的所有字符转换为小写
15  * String toUpperCase(): 使用默认语言环境,将String中的所有字符转换为大写
16  * String trim(): 返回字符串的副本,忽略前导空白和尾部空白
17  * boolean equals(Object obj ):比较字符串的内容是否相同
18  *
19  * boolean equalsUIgnoreCase(String anotherString):与equals 方法类似,忽略大小写
20  * String concat(String str):将指定字符串连接到此字符串的结尾。 等价于 “+”
21  * int compareTo(String anotherString):比较两个字符串的大小
22  * String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取
23  *      到最后的一个字符串
24  * String substring(int beginIndex,int endIndex):返回一个新字符串,它是此字符串从beginIndex开始
25  * 截取到endIndex(不含)的一个字符串
26  *
27  *
28  *
29  *
30  * @author Bytezero1·zhenglei!    Email:420498246@qq.com
31  * create  2021-10-22 8:08
32  */
33 public class StringMethodTest {
34 
35     @Test
36     public void test2(){
37         String s1 = "HelloWorld";
38         String s2 = "helloworld";
39         System.out.println(s1.equals(s2));//false
40         System.out.println(s1.equalsIgnoreCase(s2)); //true 忽略大小写
41 
42         String s3 = "abc";
43         String s4 = s3.concat("def");
44         System.out.println(s4);   //abcdef
45 
46         String s5 = "abc";
47         String s6 = new String("abe");
48         System.out.println(s5.compareTo(s6)); // -2 涉及到字符串的排序
49 
50        String s7 = "上海东方明珠";
51         String s8 = s7.substring(2);
52         System.out.println(s7);   //上海东方明珠
53         System.out.println(s8);   //东方明珠
54 
55         String s9 = s7.substring(2, 4);
56         System.out.println(s9);   //东方
57 
58 
59     }
60 
61 
62     @Test
63     public void test1(){
64         String s1 = "HelloWorld";
65         System.out.println(s1.length()); //10
66         System.out.println(s1.charAt(0));//h
67         System.out.println(s1.charAt(9));//d
68 
69        // System.out.println(s1.charAt(10));//异常: StringIndexOutOfBoundsException
70 
71         System.out.println(s1.isEmpty());//false
72 //        s1 = "";
73 //        System.out.println(s1.isEmpty());//true
74 
75         String s2 = s1.toLowerCase();
76         System.out.println(s1); //HelloWorld  不可变性,仍然为原来的字符串
77         System.out.println(s2); //helloworld  改为小写的
78 
79         String s3 = s1.toUpperCase();
80         System.out.println(s1); //HelloWorld  不可变性,仍然为原来的字符串
81         System.out.println(s3);//HELLOWORLD 改为大写
82 
83         String  s4 = "   he  ll o world   ";
84         String s5 = s4.trim();
85         System.out.println("------"+s4+"-------"); //------   he  ll o world   -------
86         System.out.println("------"+s5+"-------"); //------he  ll o world-------
87 
88 
89 
90 
91     }
92 
93 }

 

posted on 2021-10-22 09:00  Bytezero!  阅读(50)  评论(0编辑  收藏  举报