黑马程序员-------String StringBuffer StringBuilder

来源:互联网 发布:几款进销存软件好坏 编辑:程序博客网 时间:2024/04/29 13:38

----------- android培训java培训、java学习型技术博客、期待与您交流! ------------

String 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package day13;  
  2. /** 
  3.  * String 介绍 
  4.  * @author hao 
  5.  * 
  6.  */  
  7. public class StringDemo {  
  8.     public static void main(String[] args) {  
  9.         String s1 = "aslkfjdl";  
  10.         //s1 是一个类类型变量,"aslkfjdl"是一个对象。  
  11.         //字符串的最大特点:一旦被初始化就不可以被改变  
  12.           
  13.         String s2 = new String("aslkfjdl");  
  14.           
  15.         /* 
  16.          * s1和s2有什么区别 
  17.          * s1在内存中有一个对象 
  18.          * s2在内存中有两个对象 
  19.          */  
  20.         System.out.println(s1==s2); //false  
  21.         System.out.println(s1.equals(s2));//true   
  22.         //String类复写Object类的equals方法   比对象  
  23.     }  
  24. }  

=============================================================================

字符串的方法

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

1.字符串的反转

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package day13;  
  2. /** 
  3.  * 将一个字符串反转,将字符串指定的部分进行反转 "abcdefg""abfedcg" 
  4.  * @author hao 
  5.  *思路: 
  6.  *1.曾经学习过对数组的元素进行反转 
  7.  *2.将字符串变成数据,对数组进行反转 
  8.  *3.将反转的字符数组变成字符串 
  9.  *4.只要将或反转的部分的开始和结束的位置作为参数传递即可 
  10.  */  
  11. public class ReverseDemo {  
  12.   
  13.     public static String getReverse(String str, int begin,int end)  
  14.     {  
  15.         //1.将字符串变成字符数组  
  16.         char[] arr = str.toCharArray();  
  17.         //2.将字符数组进行反转  
  18.         char[] arr1 =reverse(arr,begin,end);  
  19.         //3.将字符数组变成字符串  
  20.         return new String(arr1);  
  21.     }  
  22.     public static String getReverse(String str)  
  23.     {  
  24.           
  25.         return getReverse(str,0,str.length()-1);  
  26.           
  27.     }  
  28.     //交换给定范围的字符数组  
  29.     private static char[] reverse(char[] arr,int begin,int end) {  
  30.           
  31.         for(;begin<end;begin++,end--)  
  32.         {  
  33.             remove(arr,begin,end);  
  34.         }  
  35.         return arr;  
  36.     }  
  37.     //交换两个字符  
  38.     private static void remove(char[] arr, int begin, int end) {  
  39.               
  40.         char temp = arr[begin];  
  41.         arr[begin] = arr[end];  
  42.         arr[end] = temp;  
  43.           
  44.           
  45.     }  
  46.       
  47.     public static void main(String[] args) {  
  48.         String str= "abcdef";  
  49.         System.out.println(getReverse(str,2,4));  
  50.         System.out.println(getReverse(str));  
  51.     }  
  52. }  

2.模拟trim方法

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package day13;  
  2. /** 
  3.  * 模拟一个trim方法,去除字符串两端的空格 
  4.  * @author hao 
  5.  * 
  6.  *思路: 
  7.  *1.判断字符串第一个位置是否是空格,如果是继续向下判断,直到不是空格为止 
  8.  *  结尾处判断空格也是如此 
  9.  *2.当开始和结尾都判断不是空格时,就是要获取字符串 
  10.  */  
  11. public class TrimDemo {  
  12.     public static String trimTest(String str)  
  13.     {  
  14.         int begin = 0,end = str.length()-1;  
  15.           
  16.         while(begin<=end && str.charAt(begin)==' ')  
  17.             begin++;  
  18.         System.out.println("begin="+begin);  
  19.           
  20.         while(begin<= end && str.charAt(end)==' ')  
  21.             end--;  
  22.         System.out.println("end="+end);  
  23.           
  24.         return str.substring(begin,end+1);  
  25.     }  
  26.       
  27.     public static void main(String[] args)  
  28.     {  
  29.         String s = "  hao  ";  
  30.           
  31.         System.out.println(trimTest(s));  
  32.     }  
  33. }  

3. 获取一个字符串在另一个字符串中出现的次数

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package day13;  
  2. /** 
  3.  * 获取一个字符串在另一个字符串中出现的次数 
  4.  * @author hao 
  5.  *"abhaoledahaolvhaopp" 
  6.  *思路: 
  7.  *1.定义一个计数器 
  8.  *2.获取hao第一次出现的位置 
  9.  *3.从第一个出现的位置后剩余的字符串中继续获取hao出现的位置 
  10.  *      每获取一次就计数一次 
  11.  *4.当获取不到是,计数完成 
  12.  */  
  13. public class GetStringTime {  
  14.       
  15.     public static int getTimes(String s1,String key)  
  16.     {  
  17.         int count = 0;  
  18.         while(s1.length()>=key.length())  
  19.         {  
  20.             int index = s1.indexOf(key);  
  21.             if(index!=-1)  
  22.             {  
  23.                 s1=s1.substring(index+key.length());  
  24.                 count++;  
  25.                 //System.out.println(count);  
  26.             }else  
  27.                 return count;  
  28.         }  
  29.         return count;  
  30.     }  
  31.       
  32.     public static int getTimes2(String str,String key)  
  33.     {  
  34.         int count =0;  
  35.         int index = 0;  
  36.         while((index=str.indexOf(key,index))!=-1)  
  37.         {  
  38.             System.out.println(index);  
  39.             index = index+key.length();  
  40.             count++;  
  41.         }  
  42.                       
  43.             return count;  
  44.     }  
  45.       
  46.     public static void main(String[] args) {  
  47.         String s1 = "abhaoledahaolvhaopp";  
  48.         String key = "hao";  
  49.         System.out.println(getTimes(s1,key));  
  50.         System.out.println(getTimes2(s1,key));  
  51.     }  
  52. }  

4.获取两个字符串中最大的相同子串

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package day13;  
  2. /** 
  3.  * 获取两个字符串中最大的相同子串,第一个动作:将短的那个串进行长度一次递减的子串打印 
  4.  * "lklj9ewpooehaodpwepo" 
  5.  * "djfhaodlleslfj" 
  6.  * @author hao 
  7.  * 
  8.  *思路: 
  9.  *  1.将短的那个子串按照长度递减的方式获取到 
  10.  *  2.将每获取到的子串去和长串比较判断是否包含  如果包含,已经找到 
  11.  */  
  12. public class LongString {  
  13.     public static String getLong(String s1,String s2)  
  14.     {     
  15.         String max,min;  
  16.         if(s1.length()> s2.length()){  
  17.             max=s1;  
  18.             min = s2;  
  19.         }  
  20.         else  
  21.         {  
  22.             min = s1;  
  23.             max = s2;  
  24.         }  
  25.               
  26.         for(int i = 0; i<min.length(); i++)  
  27.             for(int start=0, end =min.length()-i;end!=min.length()+1;start++,end++)  
  28.             {  
  29.                 String temp = min.substring(start,end);  
  30.             //  System.out.println(temp);  
  31.                 if(max.contains(temp))  
  32.                     return temp;  
  33.             }  
  34.         return "";  
  35.     }  
  36.       
  37.     public static void main(String[] args) {  
  38.         String s1 = "lklj9ewpooehaodpwepo";  
  39.         String s2 = "djfhaodlleslfj";  
  40.           
  41.         System.out.println(getLong(s2,s1));  
  42.     }  
  43. }  

==================================================================================================

StringBffer介绍与方法

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package day13;  
  2. /** 
  3.  * StringBuffer 
  4.  * @author hao 
  5.  * 
  6.  *StringBuffer 是一个容器 
  7.  *特点: 
  8.  *1.长度可以变化 
  9.  *2.可以存储多种数据 
  10.  *3.最终可以通过toString方法编程字符串 
  11.  * 
  12.  *1.存储(create) 
  13.  *      StringBuffer append():将指定数据作为参数添加到已有数据的结尾 
  14.  *      StringBuffer insert(index,数据):可以将数据添加到指定的位置 
  15.  * 
  16.  *2.删除(delete) 
  17.  *      StringBuffer delete(start,end):删除缓存区中的数据,包含start 不包含end 
  18.  *      StringBuffer deleteCharAt(index): 删除指定的字符 
  19.  * 
  20.  *3.修改(update) 
  21.  *      StringBuffer replace(start,end,string) 
  22.  *      void setCharAt(int index, char ch) 
  23.  * 
  24.  *4.获取(read) 
  25.  *      char charAt(index) 
  26.  *      int  indexOf(String str) 
  27.  *      int  lastIndexOf(String str) 
  28.  *      int  length() 
  29.  *      String substring(int start,int end) 
  30.  * 
  31.  *5.反转 
  32.  *      StingBuffer reverse() 
  33.  * 
  34.  *6. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)   
  35.  *       将字符从此序列复制到目标字符数组 dst。 
  36.  *======================================================================== 
  37.  *   
  38.  *      StringBuffer 是线程同步的 
  39.  *      StringBuilder 是线程不同步的(1.5版本出现 ) 
  40.  *      开发时建议 使用 StringBuilder 
  41.  * 
  42.  *  升级三个因素: 
  43.  *  1.提高效率 
  44.  *  2.简化书写 
  45.  *  3.提高安全性 
  46.  *   
  47.  */   
  48. public class StringBufferDemo {  
  49.       
  50.     public static void method_updata()  
  51.     {  
  52.         StringBuffer sb = new StringBuffer("abcdefg");  
  53.           
  54.         sb.replace(24"hao");  
  55.           
  56.         sop(sb);  
  57.     }  
  58.       
  59.     //删除  
  60.     public static void method_delete()  
  61.     {  
  62.         StringBuffer sb = new StringBuffer("abcdefg");  
  63.           
  64.         sb.delete(12);  
  65.     //  sb.delete(0, sb.length()); 清除缓存区  
  66.         sop(sb);  
  67.     }  
  68.       
  69.     //存储  
  70.     public static void method_create()  
  71.     {  
  72.         StringBuffer sb = new StringBuffer();  
  73.           
  74.         sb.append(88).append("hao").append("dd");  
  75.         sop(sb);  
  76.           
  77.         sb.insert(2"xx");  
  78.           
  79.         sop(sb);  
  80.           
  81. //      StringBuffer sb1 = sb.append("lll");  
  82.           
  83. //      sop(sb==sb1);// true  
  84. //      sop(sb.equals(sb1));//true  
  85.     }  
  86.       
  87.     public static void main(String[] args) {  
  88.           
  89.         method_updata();  
  90.           
  91. //      method_delete();  
  92. //      method_create();  
  93.     }  
  94.       
  95.     public static void sop(Object str)  
  96.     {  
  97.         System.out.println(str);  
  98.     }  
  99. }  

----------- android培训java培训、java学习型技术博客、期待与您交流! ------------
0 0
原创粉丝点击