Java基础 - 字符串 String

字符串就是用字符拼接成的文本值,字符串在存储上类似数组,在java语言中把字符串当做对象进行处理

创建字符串
 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5 
 6         /*
 7         * 引用字符串常量
 8         * */
 9         String a = "时间就是金钱,我的朋友。";
10         String b = "锄禾日当午";
11         String str1, str2;
12         str1 = "We are students";
13         str2 = "We are students";
14 
15         /*
16         * 利用构造方法实例化
17         * */
18         String c = new String("我爱清汤小肥羊");
19         String d = new String(c);
20 
21         /*
22         * 利用字符串数组实例化
23         * */
24         char[] charArray = {'t', 'i', 'm', 'e'};
25         String e = new String(charArray);
26 
27 
28         /*
29         * 提取字符数组中的一部分创建字符串对象
30         * */
31         char[] charArray2 = {'时', '间', '就', '是', '金', '钱'};
32         String f = new String(charArray2, 3, 2);
33         System.out.println("f: " + f);
34 
35     }
36 }

 

字符串对象方法

连接字符串

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6         * 连接字符串
 7         * */
 8         String a = "我叫李狗蛋";
 9         String b = "今年十九岁";
10         String c = a + ',' + b;
11         String d = "我来做个自我介绍:";
12         String f = d.concat(c);
13 
14         d += c;
15 
16         System.out.println("d = " + d);
17         System.out.println("f = " + f);
18     }
19 }

 

获取字符串长度

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6         * 获取字符串长度
 7         * */
 8         String num = "12345 6789";
 9         System.out.println("num 的长度: " + num.length());
10     }
11 }

 

获取字符串指定位置的字符

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6         * 获取字符串指定位置的字符
 7         * */
 8         String str = "床前明月光, 疑是地上霜";
 9         char chr = str.charAt(4);
10         System.out.println("字符串中索引位置为4的字符是:" + chr);    // 字符串中索引位置为4的字符是:光
11     }
12 }

 

查找子字符串索引位置

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6          * 查找子字符或字符串索引位置
 7          *   如果存在则返回索引位置,否则返回-1
 8          * */
 9         String str = "We are the world";
10         int index1 = str.indexOf('e');
11         int index2 = str.indexOf("e33");
12 
13         System.out.println("index1: " + index1);    // size: 1
14         System.out.println("index2: " + index2);    // size: -1
15     }
16 }

 

 

判断字符串首尾是否存在指定字符串

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6         * 判断字符串首尾是否存在指定字符串
 7         *   如果存在返回true,否则返回false
 8         * */
 9         String str = "We are the world";
10 
11         if (str.startsWith("We")) {
12             System.out.println("We 在开头");
13         }
14 
15         if (str.endsWith("world")) {
16             System.out.println("world 在末尾");
17         }
18 
19     }
20 }

 

 

将字符串转换成字符数组

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6         * 将字符串转换成字符数组
 7         *
 8         * */
 9         String str = "We are the world";
10 
11         char[] charArr = str.toCharArray();
12         for (int i = 0; i < charArr.length; i ++) {
13             System.out.println("数组第" + i + "个元素为:" + charArr[i]);
14         }
15 
16     }
17 }

 

 

判断子字符串是否存在

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6          * 判断子字符串是否存在
 7          * */
 8 
 9         String str = "今天的菜谱有:蒸羊羔,蒸熊掌,蒸鹿尾,烧花鸭,烧雏鸡,烧子鹅" +
10                 "卤煮咸鸭,酱鸡,腊肉,松花小肚";
11 
12         System.out.println(str);    // 今天的菜谱有:蒸羊羔,蒸熊掌,蒸鹿尾,烧花鸭,烧雏鸡,烧子鹅卤煮咸鸭,酱鸡,腊肉,松花小肚
13 
14         boolean request1 = str.contains("腊肉");
15         System.out.println("今天有腊肉吗?" + request1);   // 今天有腊肉吗?true
16 
17         boolean request2 = str.contains("汉堡");
18         System.out.println("今天有汉堡吗?" + request2);   // 今天有汉堡吗?false
19     }
20 }

 

 

截取字符串

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6          * 截取字符串
 7          * */
 8 
 9         // 截取身份号中的出生日期
10         String idNum = "123456198002157890";
11         String year = idNum.substring(6, 10);
12         String month = idNum.substring(10, 12);
13         String day = idNum.substring(12, 14);
14 
15         System.out.println("该身份证显示的出生日期为:");        // 该身份证显示的出生日期为:
16         System.out.println(year + "年" + month + "月" + day + "日");   // 1980年02月15日
17 
18     }
19 }

 

 

字符串替换

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6          * 字符串替换
 7          * */
 8 
 9         // 替换字符串中的错别字
10         String str = "登录功能介绍:用户输入用户名和密码之后,单击'登陆'按钮即可完成登陆操作。";
11         String restr = str.replace("陆", "录");   // 将字符串中所有的 '陆' 改为 '录'
12         System.out.println("【更改前】" + str);      // 输出原字符串
13         System.out.println("【更改后】" + restr);    // 输出更改后的字符串
14 
15     }
16 }

 

 

字符串分割

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6          * 字符串分割
 7          * */
 8 
 9         // 将菜谱中的菜品报错在一个数组中
10         String str = "今天的菜谱有:蒸羊羔,蒸熊掌,蒸鹿尾,烧花鸭,烧雏鸡,烧子鹅" +
11                 "卤煮咸鸭,酱鸡,腊肉,松花小肚";
12         String[] denal = str.split(",");
13 
14         for (int i = 0; i < denal.length; i++) {
15             System.out.println("索引" + i + "的元素:" + denal[i]);
16         }
17 
18     }
19 }

 

 

大小写转换

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6          * 大小写转换
 7          * */
 8 
 9         // 输出字符串的大小写格式
10         String str = "abc DEF";
11 
12         System.out.println(str.toLowerCase());  // 按照小写格式输出     abc def
13         System.out.println(str.toUpperCase());  // 按照大写格式输出     ABC DEF
14 
15     }
16 }

 

 

去除空白内容

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6          * 去除空白内容
 7          * */
 8 
 9         // 去掉字符串两边的空白内容
10         String str = "      abc     ";
11 
12         String shortStr = str.trim();
13         System.out.println("str的原值是: [" + str + "]");
14         System.out.println("去掉首尾空白的值:" + shortStr);
15 
16     }
17 }

 

比较字符串的内容是否相同

 1 package com.mingri.chapter_02;
 2 
 3 public class demo1 {
 4     public static void main (String[] args) {
 5         /*
 6          * 比较字符串是否相等
 7          * */
 8 
 9         // 比较字符串的内容是否相同
10         String str1 = "Hello";
11         String str2 = new String("Hello");
12         String str3 = new String("你好");
13         String str4 = str2;
14 
15 
16         System.out.println("str1 == str2 的结果:" + (str1 == str2));   // false
17         System.out.println("str1 == str3 的结果:" + (str1 == str3));   // false
18         System.out.println("str1 == str4 的结果:" + (str1 == str4));   // false
19         System.out.println("str2 == str4 的结果:" + (str2 == str4));   // true
20 
21         // equals 内容相等的时候为true
22         System.out.println("str1.equals(str2) 的结果:" + str1.equals(str2));   // true
23         System.out.println("str1.equals(str3) 的结果:" + str1.equals(str3));   // false
24         System.out.println("str1.equals(str4) 的结果:" + str1.equals(str4));   // true
25 
26     }
27 }

 

posted on 2018-11-10 15:47  奋斗中的码农  阅读(307)  评论(0编辑  收藏  举报

导航