String—常见功能

 

[html]  view plain copy
  1. class StringDemo  
  2. {  
  3.     public static void main(String[] args)  
  4.     {  
  5.         String s1 = "abc";//s1是一个类类型变量,"abc"是一个对象。  
  6.                  //字符串最大特点:一旦被初始化就不可以被改变。  
  7.         String s2 = new String("abc");  
  8.         //s1和s2有什么区别?  
  9.         //s1在内存中有一个对象  
  10.         //s2在内存中有两个对象  
  11.           
  12.         System.out.println(s1 == s2);//输出false  
  13.         System.out.println(s1.equals(s2));//输出true。String类复写了Object类中equals方法  
  14.                                                                             //该方法用于判断字符串是否相同。  
  15.     }  
  16. }  

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[html]  view plain copy
  1. /*  
  2. String类适用于描述字符串事物。  
  3. 那么他就提供了多个方法对字符串进行操作。  
  4.   
  5. 常见的操作有哪些?  
  6. 1.获取  
  7.     1.1 字符串中包含的字符数,也就是字符串的长度。  
  8.             int length();获取长度。  
  9.     1.2 根据位置获取位置上的某个字符。  
  10.             char charAt(int index)  
  11.     1.3 根据字符获取该字符在字符串中位置。  
  12.             int indexOf(int ch):返回的是ch在字符串中第一次出现的位置  
  13.             int indexOf(int ch, int fromIndex):从fromIndex指定位置开始,获取ch在字符串中出现的位置。  
  14.               
  15.             int indexOf(String str):返回的是str在字符串中第一次出现的位置  
  16.             int indexOf(String str, int fromIndex):从fromIndex指定位置开始,获取str在字符串中出现的位置。  
  17.               
  18.             int lastIndexOf(int ch);  
  19.             int lastIndexOf(String str);  
  20. 2.判断  
  21.      2.1 字符串中是否包含某一个子串。  
  22.              boolean contain(str);  
  23.              特殊之处:indexOf(str):可以索引str第一次出现  
  24.              位置,如果返回-1,表示str不在字符串中存在,  
  25.              所以,也可以用于对指定字符串判断是否包含  
  26.              而且该方法既可以判断,又可以获取出现的位置。  
  27.                
  28.      2.2 字符串中是否有内容。  
  29.            boolean isEmpty();原理是判断长度是否为0  
  30.      2.3 字符串是否是以指定内容开头。  
  31.            boolean startsWith(str);  
  32.      2.4 字符串是否是以指定内容结尾  
  33.            boolean endsWith(str);  
  34.      2.5 判断字符串的内容是否相同。复写了Object类中的equals方法。  
  35.              boolean equals(str);  
  36.      2.6 判断内容是否相同,并忽略大小写。  
  37.            boolean equalsIgnoreCase();  
  38.              
  39. 3.转换  
  40.     3.1 将字符数组转成字符串。  
  41.             构造函数:String(char[])  
  42.                 String(char[],offset,count):将字符数组中的一部分转成字符串  
  43.             静态方法:static String copyValueOf(char[]);  
  44.                 copyValueOf(char[] data,int offset,int count)                     
  45.                                   
  46.                 static String valueOf(char[]);  
  47.                                   
  48.     3.2 将字符串转成字符数组。**  
  49.           char[] toCharArray()  
  50.       
  51.     3.3 将字节数组装成字符串  
  52.             String(byte[])  
  53.             String(byte[],offset,count):将字节数组中的一部分转成字符串  
  54.       
  55.     3.4 将字符串转成字节数组  
  56.           byte[] getBytes()  
  57.             
  58.           特殊:字符串和字节数组在转换过程中,是可以指定编码表的。  
  59.       
  60.     3.5 将基本数据类型转成字符串  
  61.           static String valueOf(int);  
  62.           static String valueOf(double);  
  63.                
  64.             3+"";//String.valueOf(3);  
  65.               
  66. 4.替换  
  67.     String replace(oldchar,newchar);如果要替换的字符不存在,返回原串  
  68.   
  69. 5.切割  
  70.     String[] split(regex);  
  71.       
  72. 6.子串。获取字符串中的一部分。  
  73.      String substring(begin);  
  74.      String substring(begin,end);  
  75.        
  76. 7. 转换,去除空格,比较。  
  77.    7.1 将字符串转成大写或者小写  
  78.          String toUpperCase();  
  79.          String toLowerCase();  
  80.      
  81.    7.2 将字符串两端的多个空格去除  
  82.          String trim()  
  83.      
  84.    7.3 多两个字符串进行自然顺序的比较  
  85.          int compareTo(string);  
  86.   
  87. */  
  88.   
  89. class StringMethodDemo  
  90. {  
  91.     public static void method_7()  
  92.     {  
  93.         String s = "   Hello Java   ";  
  94.         sop(s.toLowerCase());     
  95.         sop(s.toUpperCase());  
  96.         sop(s.trim());  
  97.           
  98.         String s1 = "abc";  
  99.         String s2 = "aaa";  
  100.         sop(s1.compareTo(s2));  
  101.     }  
  102.       
  103.     public static void method_sub()  
  104.     {  
  105.         String s = "abcdef";  
  106.         sop(s.substring(2));//从指定位置到结尾,如果角标不存在,会出现数组角标越界异常  
  107.         sop(s.substring(2,4));//包含头,不包含尾  
  108.     }  
  109.       
  110.     public static void method_split()  
  111.     {  
  112.         String s = "zhangsan,lisi,wangwu";  
  113.         String[] arr = s.split(",");      
  114.           
  115.         for(int x=0; x<arr.length; x++)  
  116.         {  
  117.             sop(arr[x]);      
  118.         }  
  119.     }  
  120.       
  121.       
  122.       
  123.     public static void method_replace()  
  124.     {  
  125.         String s = "hello java";      
  126.         String s1 = s.replace('a','n');  
  127.         String s2 = s.replace("java","world");  
  128.           
  129.         sop("s="+s);//输出hello java  
  130.         sop("s1="+s1);//输出hello jnvn  
  131.         sop("s2="+s2);//输出hello world  
  132.     }  
  133.       
  134.     public static void method_trans()  
  135.     {  
  136.         char[] arr = {'a','b','c','d','e','f'};   
  137.         String s = new String(arr);  
  138.         sop("s="+s);  
  139.           
  140.         String s1 = "zxcvbnm";  
  141.         char[] chs = s1.toCharArray();  
  142.           
  143.         for(int x=0; x<chs.length; x++)  
  144.         {  
  145.             sop("sop="+chs[x]);   
  146.         }  
  147.     }  
  148.       
  149.     public static void method_is()  
  150.     {  
  151.         String str = "ArrayDemo.java";  
  152.           
  153.         sop(str.startsWith("Array"));//判断文件名称开头是否是Array单词开头  
  154.         sop(str.endsWith(".java"));  
  155.         sop(str.contains("Demo"));    
  156.     }  
  157.       
  158.     public static void method_get()  
  159.     {  
  160.         String str = "abcdea.kp";     
  161.           
  162.         //长度  
  163.         sop(str.length());  
  164.           
  165.         //根据索引获取字符  
  166.         sop(str.charAt(4));//当访问到字符串中不存在的角标时会发生数组角标越界  
  167.           
  168.         //根据字符获取索引  
  169.         sop(str.indexOf("a",3));//如果没有找到,返回-1  
  170.           
  171.         //反向索引一个字符出现位置。  
  172.         sop(str.lastIndexOf("."));  
  173.           
  174.     }  
  175.       
  176.     public static void main(String[] args)  
  177.     {  
  178.         method_7();  
  179.         // method_sub();  
  180.         //method_split();  
  181.         //method_replace();  
  182.         //method_trans();  
  183.         //method_get();  
  184.         /*  
  185.         String s1 = "abc";  
  186.         String s2 = new String("abc");  
  187.           
  188.         String s3 = "abc";  
  189.           
  190.         System.out.println(s1 == s2);//输出false  
  191.         System.out.println(s1 == s3);//输出true  
  192.         */  
  193.     }  
  194.       
  195.     public static void sop(Object obj)  
  196.     {  
  197.         System.out.println(obj);      
  198.     }  
  199. }  

----------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值