1 package cn.hello;
2
3 /*
4 * String类概述及其构造方法
5 * 字符串的特点:1字符串字面值可以看成一个字符串对象 2 字符串是常量,一旦被赋值,就不能被改变
6 * 构造方法:
7 * public String() 空构造
8 * public String(byte[] bytes) 把字节数组转为字符串
9 * public String(byte[] bytes,int offset,int length) 把字节数组转为字符串
10 * public String(char[] value) : 把字符数组转为字符串
11 * public String(char[] value, int index, int count):把字符串常量值转为字符串
12 * public String(String original):把字符串常量值转为字符串
13 *
14 * 成员方法
15 * public int length() :长度
16 * */
17
18 public class Test01 {
19 public static void main(String[] args) {
20 String s1=new String();
21 //s1="hello world";
22 System.out.println(s1.length());
23 //public String(byte[] bytes)
24 byte[] bys={97,98,99,100,101,102};
25 String s2=new String(bys);
26 System.out.println(s2);
27 String s3=new String(bys,2,3);
28 System.out.println(s3);
29
30 char[] chs={'a','b','c','d','e','艾'};
31 String s4=new String(chs);
32 System.out.println(s4);
33 }
34 }
1 package cn.hello;
2
3 /*
4 *
5 * 字符串是常量,一旦被赋值,就不能被改变
6 *
7 * "hello" 没有变化 ,但s所指的地址值发生了变化
8 * */
9
10 public class Test01 {
11 public static void main(String[] args) {
12 String s="hello";
13 System.out.println(s.hashCode());
14 s+="world";
15 System.out.println(s.hashCode());
16 System.out.println(s); //helloworld
17
18 }
19 }
1 package cn.hello;
2
3 /*
4 * String s=new String("hello") 和String s="hello"的区别
5 * */
6
7 public class Test01 {
8 public static void main(String[] args) {
9 String s1=new String("hello");
10 String s2="hello";
11
12 System.out.println(s1==s2); //比的是地址值
13 System.out.println(s1.equals(s2)); //比的是值
14
15 }
16 }
1 package cn.hello;
2
3 /*字符串的判断功能
4 * boolean equals(Object obj) :重写之后比较的是字符串的内容
5 * boolean equalsIgnoreCase(String str) :不计大小写的比较
6 * boolean contains(String str) :判断大串是否包含小串
7 * boolean startsWith(String str):
8 * boolean endsWith(String str)
9 * boolean isEmpty() :判断的是字符串内容是否为空
10 *
11 * */
12
13 public class Test01 {
14 public static void main(String[] args) {
15 String s1="";
16 System.out.println(s1.isEmpty());
17 }
18 }
1 package cn.hello;
2
3 /*
4 * 字符串的获取功能
5 * int length():获取字符串的长度
6 * char charAt(int index):获取指定索引位置的字符
7 * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引
8 * int indexOf(String str) :返回指定字符串在此字符串中第一次出现处的索引
9 * int indexOf(int ch,int fromIndex)
10 * int indexOf(String str,int fromIndex)
11 * String substring(int start) 截取字符串
12 * String substring(int start,int end)
13 *
14 * */
15
16 public class Test01 {
17 public static void main(String[] args) {
18
19 }
20 }
1 package cn.hello;
2
3 /*
4 * 遍历获取字符串中的每一个字符
5 * char charAt(int index)
6 * */
7
8 public class Test01 {
9 public static void main(String[] args) {
10 String s1="hello world take it easy ,so we can go there.";
11 for(int i=0;i<s1.length();i++){
12 System.out.print(s1.charAt(i)+", ");
13 }
14 }
15 }
1 package cn.hello;
2
3 /*
4 * 统计大小写
5 *
6 *
7 *
8 * */
9
10 public class Test01 {
11 public static void main(String[] args) {
12 String s1="HellO wOrld 12F";
13 int bigNum=0;
14 int smallNum=0;
15 int num=0;
16 for(int i=0;i<s1.length();i++){
17 char ch=s1.charAt(i);
18 if(ch>'a' && ch<'z'){
19 smallNum++;
20 }else if(ch>'A' && ch<'Z'){
21 bigNum++;
22 }else if(ch>'0' && ch<'9'){
23 num++;
24 }
25 }
26 System.out.println("small"+smallNum+", big:"+bigNum+" num: "+num);
27 }
28 }
1 package cn.hello;
2
3 /*
4 * 字符串的转换功能
5 * byte[] getBytes() :把字符串转换为字节数组
6 * char[] toCharArray() :把字符串转换为字符数组
7 * static String valueOf(char[] chs):把字符数组转为字符串
8 * static String valueOf(int i): 把int类型转为字符串
9 * String toLowerCase
10 * String toUpperCase
11 * String concat(String str)
12 *
13 * */
14
15 public class Test01 {
16 public static void main(String[] args) {
17
18 }
19 }
1 package cn.hello;
2
3 /*
4 * 字符串的其他功能
5 * 替换功能
6 * String replace(char old,char new)
7 * String replace(String old,String new)
8 *
9 * 去除字符串两端空格
10 * String trim()
11 *
12 * 按字典顺序比较两个字符串
13 * int compareTo(String str)
14 * int compareToIgnoreCase(String str)
15 *
16 * */
17
18 public class Test01 {
19 public static void main(String[] args) {
20
21 }
22 }