11--黑马程序员--技术总结之字符串

来源:互联网 发布:网络婚恋诈骗 编辑:程序博客网 时间:2024/04/26 00:30

----------------------ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------

一.基本概念

       字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为s=“a1a2···an”(n>=0)。它是编程语言中表示文本的数据类型。
      Java使用java.lang包中的String类来创建一个字符串变量,因此字符串变量是一个对象。

      1.字符串常量
      如,“你好”,“1234.987”,“weqweo”。
      2.声明字符串
      String S; 
      3.创建字符串
      使用String类的构造方法,例如:
      s = new String("we are students"):
      声明和创建可用一步完成:
      String  s = new  String ("we  are students");
      也可以用一个已创建的字符串创建另一个字符串,如:
      String  tom = String(s);
      String类还有两个较常用的构造方法。
      (1)String (char a[])
      用一个字符数组a创建一个字符串对象,如:
      char a[] = {'b','o','y'};
      String  s = new  String(a);
      上述过程相当于:
      String s = new String《¨boy’’);
      (2) Stlning(char a[],int startlndex,int count)
      提取字符数组a中的一部分字符创建一个字符串对象,参数startlndex和count分别指定在a中提取字符的起始位置和从该位置开始截取的字符个数,例如:
      char a[] = {'s','t','u','d','e','n','t'};
      String  s = new  String (a,2,3);
      上述过程相当于:
      String  s=  new  String ("ude");
      4.引用字符串常量对象
      字符串常量是对象,因此可以把字符串常量的引用赋值给一个字符串变量,例如:
      String s1,s2;
      s1 = "how are  you";
      s2 = "how are  you";
     这样,s1、s2具有相同的引用,因而具有相同的实体。
        二. 字符串的常用方法
        1.public int length()
       使用String类中的length()方法可以获取一个字符串的长度:
代码示例

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class StringDemo1 {  
  2.   
  3.     /**使用String类中的length()方法可以获取一个字符串的长度  
  4.      * @黑马ZWf  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         String s1 = "China";  
  9.         String s2 = "中国";  
  10.         System.out.println(s1.length());    //输出结果为:5  
  11.         System.out.println(s2.length());    //输出结果为:2  
  12.     }  
  13.   
  14. }  

           2.public boolean equals(String s)
          字符串对象调用String类中的equals方法,比较当前字符串对象的实体是否与参数指定的字符串s的实体相同:
 代码示例

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class StringDemo2 {  
  2.   
  3.     /**字符串对象调用String类中的equals方法,比较当前字符串对象的实体是否与参数指定的字符串s的实体相同  
  4.      * @黑马ZWF 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         String tom = new String("我是男孩");  
  9.         String boy = new String("我是男孩");  
  10.         String mary = new String("我是女孩");  
  11.         System.out.println(tom.equals(boy));    //输出结果为:true  
  12.         System.out.println(tom.equals(mary));   //输出结果为:false  
  13.     }  
  14.   
  15. }  

         注:equals()和"=="号的区别
         ==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。
        equals操作表示的两个变量是否是对同一个对象的引用,即堆中的内容是否相同。
        ==比较的是2个对象的地址,而equals比较的是2个对象的内容。
代码示例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class StringDemo3 {  
  2.   
  3.     /**equals和==的区别展示  
  4.      * @黑马ZWF  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         String tom = new String("我是男孩");  
  9.         String boy = new String("我是男孩");  
  10.         System.out.println(tom.equals(boy));    //equsls比较的是内容,所以输出结果为:true  
  11.         System.out.println(tom == boy);         //==操作比较的是地址值,所以输出结果为:false  
  12.     }  
  13.   
  14. }  

          3.public boolean startsWith(String s)和public boolean endsWith(String s)
        字符串对象调用startsWith(String s)方法,判断当前字符串对象的前缀是否是参数指定的字符串s,如: 
        String tom = "220302620629021", jerry = "21079670924022";
        tom.startsWith("220")的值是true,jerry.startsWith("220")的值是false。
        使用endsWith(Stning s)方法,判断一个字符串的后缀是否是字符串s,如:
        String tom = "220302620629021", jerry = "21079670924022";
        tom.endsWith("021")的值是true,jerry.endsWith("021")的值是false。
        字符串对象调用public boolean startsWith(String prefix,int toffset)方法,判断当前
        字符串从toffset索引处开始的前缀是否是参数指定的字符串prefix,如:
        String str = "123456";
        boolean  boo = str.startsWith("456",3);
        那么boo的值为true。
        4.public boolean regionMatches(int firstStart,String other,int otherStart,int length)
        字符串调用regionMatches(int frstStart,String other,int ortherStart,int length)方法,从当前字符串参数firstStart指定的位置开始处,取长度为length的一个子串,并将这个子串和参数other指定的一个子串进行比较。其中,other指定的子串是从参数othertStart指定的位置开始,从other中取长度为length的一个子串。如果两个子串相同该方法就返回true,否则返回false。
        使用该方法的重载方法:
        public boolean regionMatches(boolean  b,int firstStart, String other, int otherStart,int length);
       可以通过参数b决定是否忽略大小写,当b取true时,忽略大小写。
代码示例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class StringDemo3 {  
  2.   
  3.     /**使用regionMatches方法判断一个字符串中共出现了几个en。  
  4.      * @黑马ZWF  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.     int number=0;   
  9.     String s="student;entropy;engage,english,client";  
  10.     for(int k=0;k<s.length();k++) {  
  11.         if(s.regionMatches(k,"en",0,2)) {  
  12.             number++;  
  13.         }  
  14.     }   
  15.     System.out.println("number="+number);  
  16.     }  
  17. }  
        5.public int compareTo(String s)方法
        字符串对象可以使用String类中的compareTo(String s)方法,按字典序与参数s指定的字符串比较大小。如果当前字符串与s相同,该方法返回值0,如果当前字符串对象大于s,该方法返回正值:如果小于s,该方法返回负值。例如:
        String str = "abcde";
        str.compareTo("boy")小于0;
        str.compareTo("aba")大于0;
        str.compareTo("abcde")等于0。
        按字典序比较两个字符串还可以使用public int compareToIgnoreCase(String s)方法,该方法忽略大小写。
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class StringDemo4 {  
  2.   
  3.     /**使用字典排序法惊醒字符排序  
  4.      * @黑马ZWF  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.          String a[]={"door","apple","Applet","girl","boy"};  
  9.          for(int i=0;i<a.length-1;i++) {  
  10.              for(int j=i+1;j<a.length;j++) {  
  11.                  if(a[j].compareTo(a[i])<0) {        //使用字典排序法进行字符排序  
  12.                      String temp=a[i];  
  13.                      a[i]=a[j];  
  14.                      a[j]=temp;  
  15.                  }  
  16.              }   
  17.          }  
  18.          for(int i=0;i<a.length;i++) {  
  19.              System.out.print("  "+a[i]);   //输出结果为:  Applet  apple  boy  door  girl  
  20.          }  
  21.     }  
  22. }  


       6.public int indexOf(String s)
       字符串调用indexOf(String s)方法从当前字符串的头开始检索字符串s,并返回首次出现s的位置。如果没有检索到字符串s,该方法返回的值是一1 0字符串调用indexOf(String s,int startpoint)方法从当前字符串的startpoint位置处开始检索字符串s,并返回首次出现s的位置口如果没有检索到字符串s,该方法返回的值是-1。字符串调用lastlndexOf(String s)方法从当前字符串的头开始检索字符串s,并返回最后出现s的位置。如果没有检索到字符串s,该方法返回的值是-1。
代码示例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class StringDemo5 {  
  2.   
  3.     /**调用indexOf方法进行字符串的检索  
  4.      * @黑马ZWf  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         String s = "Beijing huan ying ni";  
  9.         System.out.println(s.indexOf("j"));     //输出结果为:3  
  10.         System.out.println(s.indexOf("huan"));  //输出结果为:8  
  11.         System.out.println(s.indexOf("y", 8));  //输出结果为:13  
  12.         System.out.println(s.indexOf("y", 17)); //输出结果为:-1  
  13.     }  
  14. }  

        7.public String substring(int startpoint)
        字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的startpoint处截取到字符串的末尾处所得到的字符串。字符串对象调用substring(int start,int end)方法获得一个当前字符串的子串,该子串是从当前字符串的start处截取到end处所得到的字符串,但不包括end处所对应的字符。
代码示例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class StringDemo6 {  
  2.   
  3.     /**调用substring方法截取字符串  
  4.      * @黑马ZWF  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         String s = "Beijing huan ying ni";  
  9.         System.out.println(s.substring(8, 12));     //输出结果为:huan  
  10.     }  
  11. }  

         8.public String replaceAll(String oldString, String newString)
        字符串对象s调用该方法可以获得一个串对象,这个串对象是通过用参数newString指定的字符串替换s中由oldString指定的所有字符串而得到的字符串。
        另外,字符串对象s调用public String replaceFirst(String oldstring,String newString)方法可以获得一个串对象,这个串对象是通过用参数newString指定的字符串替换s中出现的第一个oldString指定的字符串而得到的字符串。
代码示例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class StringDemo7 {  
  2.   
  3.     /**使用replaceAll方法进行文件名的后缀变化  
  4.      * @黑马ZWF  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.           String path="c:\\myfile\\2000\\result.txt";  
  9.           int index=path.lastIndexOf("\\");  
  10.           String fileName=path.substring(index+1);  
  11.           String newName=fileName.replaceAll(".txt",".java");  
  12.           System.out.println(path);  
  13.           System.out.println(fileName);  
  14.           System.out.println(newName);   
  15.     }  
  16. }  

        三.字符串与字符、字节数组
        1.字符串与字符数组
       (1)用字符数组创建字符串对象
        String类中有两个用字符数组创建字符串对象的构造方法:
        1)String(char[])  该构造方法用指定的字符数组构造一个字符串对象口
        2)String(char[],int offset,int length)用指定的字符数组的一部分,即从数组起始位置offset开始取length个字符构造一个字符串对象。
       (2)将字符串中的字符复制到字符数组
       1)public void getChars(int start,int cnd,char c[],int offset)  
       字符串调用getChars方法将当前字符串中的一部分字符复制到参数c指定的数组中。将字符串中从位置start到end-1位置上的字符复制的数组c中,并从数组c的offset处开始存放这些字符。需要注意的是,必须保证数组c能容纳下要被复制的字符。
       2)public char[] toCharArray()字符串对象调用该方法可以初始化一个字符数组,该
        数组的长度与字符串的长度相等,并将字符串对象的全部字符复制到该数组中。
代码示例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class StringDemo8 {  
  2.   
  3.     /**具体地说明getChars(int start.int end,char c[],int offset)方法的使用。  
  4.      * @黑马ZWf  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.          char c[],d[];  
  9.          String s="中国足球队击败小日本足球队";  
  10.          c=new char[2];  
  11.          s.getChars(5,7,c,0);  
  12.          System.out.println(c);     //输出结果为:击败  
  13.          d=new char[s.length()];  
  14.          s.getChars(7,12,d,0);  
  15.          s.getChars(5,7,d,5);  
  16.          s.getChars(0,5,d,7);  
  17.          System.out.println(d);     //输出结果为:小日本足球击败中国足球队  
  18.        }  
  19. }  

          2.字符串与字节数组
        (1)用字节数组创建字符串对象
        1)String (byte[])该构造方法使用平台默认的字符编码,用指定的字节数组构造一个字符串对象。
         2)String(byte[],int offset,int length)该构造方法使用平台默认的字符编码,用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一 个字符串对象
       (2)将字符串转化为字节数组
        public byte[]getBytes()    使用平台默认的字符编码,将当前字符串转化为一个字节数组。
 代码示例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class StringDemo9 {  
  2.   
  3.     /**将字符串转为数组  
  4.      * @黑马ZWF  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         byte d[]="你我他".getBytes();             
  9.         System.out.println("数组d的长度是(一个汉字占两个字节):"+d.length);  
  10.         String s=new String(d,0,2);  
  11.         System.out.println(s);  
  12.     }  
  13. }  

  

0 0