Java String类浅析

14 篇文章 0 订阅
2 篇文章 0 订阅

String类被final修饰,所以不能被继承。该类实现了 java.io.Serializable, Comparable<String>,CharSequence接口

构造方法:

               public String(); // 将value初始化为一个长度为0的字符数组

               public String(String original); //用一个字符串构造一个字符串实例

               public String(char value[ ]) ; //用字符数组构造一个字符串,实质是将字符串内容复制给value

               public String(char value[ ], int offset, int count); // 用字符数组的一部分构造一个字符串

public String(int[]codePoints,intoffset,intcount);//用ASCII码数组构造一个字符串

public String(byteascii[],inthibyte,intoffset,intcount);hibyte是每个16位unicode的高8位

public String(byteascii[],inthibyte)

public String(bytebytes[],intoffset,intlength,StringcharsetName);//charsetName为编码名称

public String(bytebytes[],intoffset,intlength,Charsetcharset) ;

public String(bytebytes[],StringcharsetName);

public String(bytebytes[],Charsetcharset);

public String(bytebytes[],intoffset,intlength);

public String(bytebytes[]);

public String(StringBufferbuffer);

public String(StringBuilderbuilder) ;

私有域: private  final char value[ ]  //String实质上是一个字符数组

               private int hash; //存储字符串的HashCode

public方法:

 静态:

public static String format(Stringformat,Object... args);//返回指定格式的字符串

public static String valueOf(Objectobj);//返回obj.toString();

public static String valueOf(chardata[]); //返回data字符串数组对应的字符串

public static String valueOf(chardata[],int offset, int count) ;//返回data字符数组指定偏移量的长度为count的字符串

public static String valueOf(booleanb);//将Boolean类型的值转化成“true”或者“false”;

public static String valueOf(charc);//将单个字符转换成字符串

public static String valueOf(inti);//将整数转换成字符串

public static String valueOf(longl);//将长整型转换成字符串

public static String valueOf(floatf);//将小数转换成字符串

public static String valueOf(doubled);//将小数转换成字符串

非静态:

public int length() ; //返回字符串长度,实质是return value.length;

public boolean isEmpty() ;//判断字符串是否为空,实质是判断value长度是否为0

public char charAt(intindex);//求某个指定位置的字符,实质是value[index]

public int codePointAt(intindex);//返回指定位置字符的编码,实质是Character.codePointAtImpl(value, index, value.length);

public int codePointBefore(intindex);//返回指定位置之前的字符编码,也是对value[] 操作

public int codePointCount(intbeginIndex,intendIndex);//返回范围之内的unicode point数量

public int offsetByCodePoints(intindex,intcodePointOffset);通过codePointOffset指定偏移量

public void getChars(intsrcBegin,intsrcEnd,chardst[],intdstBegin);//将字符串指定范围内的字符复制到目的字符数组

public void getBytes(intsrcBegin,intsrcEnd,bytedst[],intdstBegin);//复制到目的byte数组,只复制低8位,8八位丢弃

public byte[] getBytes(StringcharsetName);//通过指定的编码格式将该字符串转换成对应的byte数组存储

public byte[] getBytes(Charsetcharset);//同上

public byte[] getBytes();//通过平台默认的编码格式转换字符串到一个byte数组里

public boolean equals(ObjectanObject);//比较两个字符串是否相同

public boolean contentEquals(StringBuffersb) ;//同StringBuffer的内容比较是否相同

public boolean contentEquals(CharSequencecs);//同CharSequence的内容比较是否相同

public boolean equalsIgnoreCase(StringanotherString); //同另一个字符串比较,忽略大小写

public int compareTo(StringanotherString);//与另一个字符串比较,如果该字符串是anotherString字符串的前缀,则返回value.length-anotherString.length;

如果anotherString是该字符串的前缀,则返回anotherString.length-value.length;

否则返回两个字符串从头开始第一个不一样的字符之差

public int compareToIgnoreCase(Stringstr) ;//与str比较,忽略大小写

                                           

public boolean regionMatches(inttoffset,Stringother,intooffset,int len);//比较两个字符串的子串,toffset指定本字符串的偏移量,other指定比较的另一个字符串,ooffset指定Other的偏移量,len指定比较的长度

public boolean regionMathces(boolean ignoreCase,int toffset,String other,int ooffset,int len);//同上,指定了是否忽略大小写

public boolean startsWith(Stringprefix,inttoffset);//判断prefix是否是从指定位置起的字串的前缀

public boolean startsWith(Stringprefix);//同上,默认toffset为0

public boolean endsWith(Stringsuffix) ;//suffix是否是该字符串的结尾部分

public int indexOf(intch);//返回指定字符(ch)在该字符串中第一次出现的位置

public int indexOf(intch,intfromIndex);//同上,但是指定了搜索的起始位置

public int lastIndexOf(intch,intfromIndex);//返回ch最后出现的位置,从fromIndex往前搜索

public int lastIndexOf(intch);//返回ch最后出现的位置,从value.length-1往前搜索

public int indexOf(Stringstr,intfromIndex);//返回指定位置开始出现的str的起始位置

public int indexOf(Stringstr) ;//返回从头开始出现的str的起始位置

public int lastIndexOf(Stringstr);//返回str最后出现的位置

public int lastIndexOf(Stringstr,intfromIndex)//从fromindex往前搜索,返回str最后出现的位置

public String substring(intbeginIndex);//从指定位置开始的字串

public String substring(intbeginIndex,intendIndex) ;//指定范围内的字串

public CharSequence subSequence(intbeginIndex,intendIndex);//指定范围内的字符序列

public String concat(Stringstr);//在字符串后面再连接一个字符串

public String replace(charoldChar,charnewChar);// 将字符串中的oldChar全部替换成newChar并返回新的字符串

public boolean matches(Stringregex);//是否满足正则表达式

public boolean contains(CharSequences);//是否包含s

public String replaceFirst(Stringregex,Stringreplacement) ;//将第一个满足正则表达式的字串替换成replacement

public String replaceAll(Stringregex,Stringreplacement);//将所有满足正则表达式的子串替换成replacement

public String replace(CharSequencetarget,CharSequencereplacement) ;//将target字串替换成replacement

public String[] split(Stringregex,intlimit);//分割字符串,limit指定起始位置,遇到regex则进行分割

public String[] split(Stringregex) ;//分割字符串,limit=0;

public String toLowerCase(Localelocale);//通过规则locale转成小写,映射基于unicode编码,由于字符映射并非是1:1 的,所以结果字符串的长度可能与被转的字符串长度不一致

public String toLowerCase();//localse为默认值

public String toUpperCase(Localelocale) ;//转大写

public String toUpperCase();//locale为默认值

public String trim();//去掉首位的空白符

public char[] toCharArray();// 返回字符数组



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值