String类(获取,转换,判断,比较)

  1 package cn.itcast.p1.string.demo;
  2 
  3 import java.util.Iterator;
  4 
  5 import com.sun.org.apache.xpath.internal.operations.Equals;
  6 
  7 public class StringMethodDemo {
  8 
  9     public static void main(String[] args) {
 10         /*
 11          * 按照面向对象的思想对字符串进行功能分类。
 12          * "abcd"
 13          * 
 14          * 1,获取:
 15          * 1.1获取字符串中字符的个数(长度)。
 16          *     int length();
 17          * 1.2根据位置获取字符。
 18          *     char charAt(int index)\
 19          * 1.3根据字符获取在字符串中的第一次出现的位置。***(重点)
 20          *     int indexOf(int ch)
 21          *     int indexOf(int ch,int fromIndex):从指定位置进行ch的查找第一次出现位置
 22          *     int indexOf(String str)
 23          *     int indexOf(String str,fromIndex)
 24          *     根据字符获取在字符串中的第一次出现的位置。***(重点)
 25          *     int lastIndexOf(int ch)
 26          *     int lastIndexOf(int ch,int fromIndex):从指定位置进行ch的查找第一次出现位置
 27          *     int lastIndexOf(String str)
 28          *     int lastIndexOf(String str,fromIndex)
 29          * 1.4获取字符串中一部分字符串。也叫子串。
 30          *     String substring(int beginIndex,int endIndex)
 31          *     String substring(int beginIndex)
 32          * 
 33          * 
 34          * 2, 转换。
 35          *       2.1将字符串变成字符串数组(字符串的切割)
 36          *           String[] split(String regex)涉及到正则表达式
 37          *       2.2将字符串变成字符数组。
 38          *           char[] toCharArray()
 39          *       2.3将字符串变成字节数组。
 40          *           byte[] getBytes()
 41          *       2.4将字符串中的字母转成大小写。
 42          *           String toUpperCase():大写
 43          *           String toLowerCase():小写
 44          *       2.5将字符串中的内容进行替换
 45          *           String replace(char oldChar,char newChar)
 46          *           String replace(String s1,String s2) 
 47          *       2.6将字符串两端的空格去除。
 48          *           String trim();
 49          *       2.7将字符串进行连接。
 50          *           String concat(String str) 
 51          * 3,判断
 52          *       3.1两个字符串内容是否相同?
 53          *           boolean equals(Object obj)
 54          *           boolean equalsIgnoreCase(String str) 忽略大小写比较字符串内容
 55          *       3.2字符串中是否包含指定字符串?
 56          *           boolean contains(String str)
 57          *       3.3字符串是否以指定字符串开头。是否以指定字符串结尾
 58          *           boolean startsWith(String str)
 59          *           boolean endsWith(String str)
 60          *           
 61          * 4,比较   
 62          *      4.1       
 63          */
 64         stringMethodDemo_1();
 65         
 66         stringMethodDemo_2();
 67         
 68         stringMethodDemo_3();
 69         
 70         stringMethodDemo_4();
 71 
 72     }
 73 
 74     private static void stringMethodDemo_4() {
 75         System.out.println("a".compareTo("c"));//返回大于0 等于0 小于0
 76         System.out.println("abc".compareTo("apq"));//依赖对象自身特点完成比较
 77         
 78     }
 79 
 80     private static void stringMethodDemo_3() {//字符串判断功能
 81         String s = "abc";
 82         System.out.println(s.equals("ABC"));
 83         System.out.println(s.equalsIgnoreCase("ABC"));
 84         System.out.println(s.contains("bc"));//可以用indexOf,contains 1.5版本后该方法才有
 85         
 86         String str = "ArrayDemo.java";
 87         System.out.println(str.startsWith("Array"));
 88         System.out.println(str.endsWith("java"));
 89         System.out.println(str.contains("Demo"));
 90         
 91     }
 92 
 93     private static void stringMethodDemo_2() {//字符串转换功能
 94         String s = "张三,李四,王五";
 95         String[] arr = s.split("李四");//"."这个点是正则里面用到的 不能用于切割 要用 \\.
 96         for (int i = 0; i < arr.length; i++) {
 97             System.out.println(arr[i]); 
 98             
 99         }
100         
101         char[] chs = s.toCharArray();
102         for (int i = 0; i < chs.length; i++) {
103             System.out.println(chs[i]); 
104             
105         }
106         s = "ab你";
107         byte[] bytes = s.getBytes();
108         for (int i = 0; i < bytes.length; i++) {
109             System.out.println(bytes[i]); 
110             
111         }//97 98 -60 -29 
112         
113         System.out.println("ABc".toUpperCase());
114         
115         System.out.println("java".replace('a', 'o'));//jovo
116         System.out.println("java".replace('q', 'e'));//java
117         
118         String s1 = "java";
119         String s2 = s1.replace('q', 'z');
120         System.out.println(s1==s2);//true
121         
122         System.out.println("-"+"abc".trim()+"-");
123         System.out.println("-"+"  ab  c".trim()+"-"); 
124         
125         System.out.println("abc".concat("kk"));//abckk更专业点
126         System.out.println("abc"+"kk");//abckk
127         
128         System.out.println(String.valueOf(4)+1);
129     }
130 
131     public static void stringMethodDemo_1() {//字符串获取功能
132         // TODO Auto-generated method stub
133         String s = "abcdae";
134         System.out.println("length:"+s.length());//6
135         System.out.println("char:"+s.charAt(2));//c
136     //    System.out.println("char:"+s.charAt(20));// java.lang.StringIndexOutOfBoundsException
137         System.out.println("index:"+s.indexOf('a'));//0
138         System.out.println("index:"+s.indexOf('k'));//-1我们可以根据-1来判断该字符或字符串是否存在
139         System.out.println("iastIndex:"+s.lastIndexOf('a'));//4
140         System.out.println(s.indexOf(97));//打印0
141         
142         System.out.println("substring:"+s.substring(2,4));
143     }
144 
145 }
View Code

 

posted @ 2021-10-13 20:54  doremi429  阅读(11)  评论(0编辑  收藏  举报