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

Java String类

时间:2017-05-20 01:17:16      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:span   []   fse   class   定义   substring   大写   void   ignore   

 1 package demo03;
 2 
 3 //String类的构造方法
 4 public class StringDemo {
 5     public static void main(String[] args) {
 6         
 7         String str1 = "str1";                    //定义方式1
 8         String str2 = new String("str1");        //定义方式2
 9         
10         System.out.println(str1);
11         System.out.println(str2);
12         
13         //false str1==str2 比较的是内存地址
14         System.out.println(str1==str2);            
15         //true String类 重写equals() 比较的是每个字符
16         System.out.println(str1.equals(str2));
17         
18         byte[] by = {97,98,99,100};
19         String str3 = new String(by);            //构造方法 String(byte[] bytes) 传字节数组
20         System.out.println(str3);                //abcd
21 
22         String str4 = new String(by,1,3);        //构造方法 String(byte[] bytes int offset, int length)
23         System.out.println(str4);                //bcd
24         
25         char[] charArray = {‘i‘,‘j‘,‘k‘};
26         String str5 = new String(charArray);    //构造方法 String(char[] value) 传字符数组
27         System.out.println(str5);                //ijk
28         
29         String str6 = new String(charArray,1,2);//构造方法 String(char[] value, int offset, int count)
30         System.out.println(str6);                //jk
31         
32         
33         
34         
35     }
36 }
 1 package demo03;
 2 
 3 public class StringDemo1 {
 4     public static void main(String[] args) {
 5         String s1 = "abcdefgabcdefg";
 6         
 7         int length = s1.length();                    //获得字符串长度
 8         System.out.println(length);                  //14
 9         
10         String s2 = s1.substring(0,3);               //截取字符串 两个参数
11         String s3 = s1.substring(7);                 //一个参数
12         System.out.println(s2);                      //abc
13         System.out.println(s3);                      //abcdefg
14         
15         boolean b1 = s1.startsWith("abc");           //是否以指定字符串开始
16         System.out.println(b1);                      //true
17         
18         boolean b2 = s1.endsWith("efg");             //是否以指定字符串结尾
19         System.out.println(b2);                      //true
20         
21         boolean b3 = s1.contains("gab");             //是否包含指定字符串
22         System.out.println(b3);                      //true
23         
24         int index1 = s1.indexOf(‘c‘);             //返回指定字符第一次出现的索引
25         int index2 = s1.indexOf(‘x‘);
26         System.out.println(index1);               //2
27         System.out.println(index2);               //-1
28         
29         byte[] bytes = s1.getBytes();            //字符串转为字节数组
30         for (int i = 0; i < bytes.length; i++) {
31             System.out.println(bytes[i]);
32         }
33         
34         char[] ch = s1.toCharArray();               //字符串转为字符数组
35         for (int i = 0; i < ch.length; i++) {
36             System.out.println(ch[i]);
37         }
38         
39         String s5 = "Abc";
40         String s6 = "abc";
41         boolean b5 = s5.equals(s6);                //比较字符串
42         boolean b6 = s5.equalsIgnoreCase(s6);      //比较字符串 忽略大小写
43         System.out.println(b5);                    //false
44         System.out.println(b6);                    //true
45         
46         
47         
48     }
49 }

 demo

 1 package demo03;
 2 
 3 public class StringLianXi {
 4     public static void main(String[] args) {
 5         getCount("fhdkfADfDf123F");
 6         System.out.println(toConvert("aVdfDfd"));
 7         System.out.println(getStringCount("helloabc,abc,abcabcfjdklsfjd", "abc"));
 8     }
 9     
10     //获取指定字符串中 大写字母, 小写字母, 数字 的个数
11     public static void getCount(String s) {
12         int upper = 0;
13         int lower = 0;
14         int digit = 0;
15         
16         for (int i = 0; i < s.length(); i++) {
17             char c = s.charAt(i);
18             
19             /*if (c>=65 && c<=90) {
20                 upper++;
21             } else if (c>=97 && c<=122) {
22                 lower++;
23             } else if (c>=48 && c<=57) {
24                 digit++;
25             }*/
26             
27             if (c>=‘A‘ && c<=‘Z‘) {
28                 upper++;
29             } else if (c>=‘a‘ && c<=‘z‘) {
30                 lower++;
31             } else if (c>=‘0‘ && c<=‘9‘) {
32                 digit++;
33             }
34         }
35         
36         System.out.println(upper);
37         System.out.println(lower);
38         System.out.println(digit);
39     }
40     
41     //将指定字符串第一个字母大写, 其它字母小写
42     public static String toConvert(String s) {
43         String first = s.substring(0,1);
44         String after = s.substring(1);
45         first = first.toUpperCase();
46         after = after.toLowerCase();
47         
48         return first+after;
49     }
50     
51     //查询指定字符串中出现其它字符串的次数
52     public static int getStringCount(String s, String key) {
53         int count = 0;
54         int index = 0;
55         
56         while ((index = s.indexOf(key)) != -1) {
57             count++;
58             s = s.substring(index+key.length());
59         }
60         
61         return count;
62     }
63     
64 }

 

Java String类

标签:span   []   fse   class   定义   substring   大写   void   ignore   

原文地址:http://www.cnblogs.com/lwn007/p/6880598.html

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