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

Java基础语法 - 字符串

时间:2018-11-10 16:47:03      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:对象   字符串转换   string   print   style   out   end   文本   dex   

字符串就是用字符拼接成的文本值,字符串在存储上类似数组,在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 }

 

字符串对象方法

A:连接字符串

 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 }

 

B:获取字符串长度

 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 }

 

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

 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 }

 

D:查找子字符串索引位置

 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 }

 

 

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

 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 }

 

 

F:将字符串转换成字符数组

 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 }

 

Java基础语法 - 字符串

标签:对象   字符串转换   string   print   style   out   end   文本   dex   

原文地址:https://www.cnblogs.com/CongZhang/p/9939609.html

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