java.lang.String字符串类

一、String类

  1. String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
  2. 字符串是一个特殊的对象
  3. 字符串是常量,一旦初始化就不可以被改变
  4. String str = “abc”;和String str1 = new String(“abc”);的区别在于:
  5. str==str1----false    str.equals(str1)----true
  6. str在内存中有一个对象“abc”
  7. str1在内存中有两对象“abc”和new String
  8. String类重写了Object类中的equals方法,该方法用于判断字符串内容是否相等
  9. “abc”是一个常量,存放于方法区的常量池

二、常用方法

  1. /** 
  2.  * 
  3.  */  
  4. public final class String  
  5.     implements java.io.Serializable, Comparable<String>, CharSequence  
  6. {  
  7.     private final char value[];//字符数组  
  8.   
  9.     private final int offset;  
  10.   
  11.     private final int count;//length  
  12.   
  13.     private int hash;  
  14.   
  15.     private static final long serialVersionUID = -6849794470754667710L;  
  16.   
  17.     //构造函数  
  18.     //初始化一个新创建的 String 对象,使其表示一个空字符序列。  
  19.     public String() {  
  20.         this.offset = 0;  
  21.         this.count = 0;  
  22.         this.value = new char[0];  
  23.     }  
  24.     //初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。  
  25.     public String(String original) {  
  26.         int size = original.count;  
  27.         char[] originalValue = original.value;  
  28.         char[] v;  
  29.         if (originalValue.length > size) {  
  30.                 int off = original.offset;  
  31.                 v = Arrays.copyOfRange(originalValue, off, off+size);  
  32.         } else {  
  33.             v = originalValue;  
  34.         }  
  35.         this.offset = 0;  
  36.         this.count = size;  
  37.         this.value = v;  
  38.     }  
  39.     //分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。  
  40.     public String(char value[]) {  
  41.         this.offset = 0;  
  42.         this.count = value.length;  
  43.         this.value = StringValue.from(value);  
  44.     }  
  45.     //分配一个新的 String,它包含取自字符数组参数一个子数组的字符。  
  46.     public String(char value[], int offset, int count) {  
  47.         if (offset < 0) {  
  48.             throw new StringIndexOutOfBoundsException(offset);  
  49.         }  
  50.         if (count < 0) {  
  51.             throw new StringIndexOutOfBoundsException(count);  
  52.         }  
  53.         if (offset > value.length - count) {  
  54.             throw new StringIndexOutOfBoundsException(offset + count);  
  55.         }  
  56.         this.offset = 0;  
  57.         this.count = count;  
  58.         this.value = Arrays.copyOfRange(value, offset, offset+count);  
  59.     }  
  60.     //通过使用指定的字符集解码指定的 byte 子数组,构造一个新的 String。  
  61.     public String(byte bytes[], int offset, int length, String charsetName)throws UnsupportedEncodingException  
  62.     {  
  63.         if (charsetName == null)  
  64.             throw new NullPointerException("charsetName");  
  65.         checkBounds(bytes, offset, length);  
  66.         char[] v = StringCoding.decode(charsetName, bytes, offset, length);  
  67.         this.offset = 0;  
  68.         this.count = v.length;  
  69.         this.value = v;  
  70.     }  
  71.     //通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。  
  72.     public String(byte bytes[], String charsetName)throws UnsupportedEncodingException  
  73.     {  
  74.         this(bytes, 0, bytes.length, charsetName);  
  75.     }  
  76.     public String(byte bytes[], int offset, int length) {  
  77.       
  78.     }  
  79.   
  80.     public String(byte bytes[]) {  
  81.         this(bytes, 0, bytes.length);  
  82.     }  
  83.   
  84.     public String(StringBuffer buffer) {  
  85.         String result = buffer.toString();  
  86.         this.value = result.value;  
  87.         this.count = result.count;  
  88.         this.offset = result.offset;  
  89.     }  
  90.   
  91.     public String(StringBuilder builder) {  
  92.         String result = builder.toString();  
  93.         this.value = result.value;  
  94.         this.count = result.count;  
  95.         this.offset = result.offset;  
  96.     }  
  97.   
  98.     //获取方法************************************  
  99.     //1,返回此字符串的长度  
  100.     public int length() {  
  101.         return count;  
  102.     }  
  103.     //2,返回指定索引处的 char 值  
  104.     public char charAt(int index) {  
  105.         if ((index < 0) || (index >= count)) {  
  106.             throw new StringIndexOutOfBoundsException(index);  
  107.         }  
  108.         return value[index + offset];  
  109.     }  
  110.     //3,返回指定字符在此字符串中第一次出现处的索引。  
  111.     public int indexOf(int ch) {  
  112.         return indexOf(ch, 0);  
  113.     }  
  114.     //4,返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索  
  115.     public int indexOf(int ch, int fromIndex) {  
  116.         //....  
  117.     }  
  118.     //5,返回指定子字符串在此字符串中第一次出现处的索引  
  119.     public int indexOf(String str)  
  120.     //6,返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始  
  121.     public int indexOf(String str, int fromIndex)  
  122.     //7,返回指定字符在此字符串中最后一次出现处的索引。  
  123.     public int lastIndexOf(int ch)  
  124.     //8,返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。  
  125.     public int lastIndexOf(int ch, int fromIndex)  
  126.     //9,返回指定子字符串在此字符串中最右边出现处的索引  
  127.     public int lastIndexOf(String str)  
  128.     //10,返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。  
  129.     public int lastIndexOf(String str, int fromIndex)  
  130.   
  131.     /** 
  132.     *所有查找字符或字符串位置的如果没有,则返回-1 
  133.     *charAt函数中的index如果超过字符串的长度则会出现字符串角标越界异常IndexOutOfBoundsException是RuntimeException的子类 
  134.     */  

  1. {  
  2.     //判断方法*****************************************  
  3.   
  4.     //测试此字符串是否以指定的前缀开始。  
  5.     public boolean startsWith(String prefix)  
  6.     //测试此字符串从指定索引开始的子字符串是否以指定前缀开始。  
  7.     public boolean startsWith(String prefix, int toffset)  
  8.     //测试此字符串是否以指定的后缀结束。  
  9.     public boolean endsWith(String suffix)  
  10.     //当且仅当 length() 为 0 时返回 true。 即可以判断字符串是否为空  
  11.     public boolean isEmpty()  
  12.     //当且仅当此字符串包含指定的 char 值序列时,返回 true。 即可以判断字符串中是否包含指定字符串,也可以用indexOf(String str)来判断  
  13.     //接口 java.lang.CharSequence  
  14.     //所有已知子接口:Name  
  15.     //所有已知实现类:CharBuffer, Segment, String, StringBuffer, StringBuilder  
  16.     public boolean contains(CharSequence s)  
  17.     //判断两个字符串内容是否相同  
  18.     public boolean equals(Object anObject)  
  19.     //将此 String 与另一个 String 比较,不考虑大小写  
  20.     public boolean equalsIgnoreCase(String anotherString)  

  1. {     
  2.     //转换方法***********  
  3.   
  4.     //1,将字符数组,转换成字符串  
  5.     public String(char[] value)  
  6.     分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。该字符数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串-----将字符数组,转换成字符串  
  7.     public String(char[] value, int offset,int count)  
  8.     //2,将字符数组的一部分,转换成字符串offset是起始位置,count是长度  
  9.     public static String copyValueOf(char[] data)  
  10.     //3,返回指定数组中表示该字符序列的 String。  
  11.     public static String copyValueOf(char[] data, int offset, int count)  
  12.     //4,返回指定数组中表示该字符序列的 String  
  13.     public static String valueOf(char[] data,int offset, int count)  
  14.     public static String valueOf(char[] data)  
  15.        
  16.     //5,将字符串转换成字符数组  
  17.     public char[] toCharArray()  
  18.     //6,将字节数组转换成字符串  
  19.     public String(byte[] bytes)  
  20.     public String(byte[] bytes, int offset,int length)  
  21.     //7,将字符串转换成字节数组,使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中  
  22.     public byte[] getBytes()  
  23.     //8,使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。  
  24.     public byte[] getBytes(String charsetName)  
  25.     //9,将基本数据类型转换成字符串  
  26.     public static String valueOf(int i)。。。。。  
  27. 注意:字符串和字节数组在转换过程中,是可以指定编码表的  


  1. {  
  2.     //替换方法*********************  
  3.   
  4.     //返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。  
  5.     public String replace(char oldChar,char newChar)  
  6.     //用新字符串替换原有字符串,如果要替换的部分不存在就返回原字符串  
  7.     public String replace(CharSequence target,CharSequence replacement)  
  8.     //使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串  
  9.     public String replaceAll(String regex, String replacement)  

  1. //切割方法********************  
  2.   
  3. //根据给定规则的匹配拆分此字符串。  
  4. public String[] split(String regex) 

  1. //获取字符串中子串*******************  
  2.   
  3. //返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。  
  4. public String substring(int beginIndex)  
  5. //包含头不包含尾   
  6. public String substring(int beginIndex,int endIndex)  
  1. //转换,去除空格,比较*******************  
  2.   
  3.   
  4. //1,将字符串转换成大写小写  
  5.    public String toLowerCase()//转成小写  
  6.    public String toUpperCase()//转成大写  
  7. //2,将字符串两端多余的空格去除  
  8.    public String trim()  
  9. //3,对两个字符串进行自然顺序的比较  
  10.    public int compareTo(String anotherString)  
  11.    大于就返回正数  
  12.    小于返回负数  
  13.    等于返回0  
  14.    //比较时不考虑大小写  
  15.    public int compareToIgnoreCase(String str) 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值