final类,无子类。

类内定义了char数组value[],私有,不可修改。
String的长度,length()。
判空,isEmpty()。
索引index处的字符,charAt(index)。
字符(ch)所在的索引,indexOf(ch)

一、构造函数

(1)无参构造,value[]长度为0
  1. public String(){this.value = new char[0];}
(2)用String构造
  1. public String(String original) {
  2. this.value = original.value;
  3. this.hash = original.hash;
  4. }
(3)用char数组构造
  1. public String(char value[]) {
  2. this.value = Arrays.copyOf(value, value.length);
  3. }
  4. 数组完全拷贝
  5. public String(char value[], int offset, int count) {
  6. if (offset < 0) {
  7. throw new StringIndexOutOfBoundsException(offset);
  8. }
  9. if (count < 0) {
  10. throw new StringIndexOutOfBoundsException(count);
  11. }
  12. // Note: offset or count might be near -1>>>1.
  13. if (offset > value.length - count) {
  14. throw new StringIndexOutOfBoundsException(offset + count);
  15. }
  16. this.value = Arrays.copyOfRange(value, offset, offset+count);
  17. }
  18. 数组部分拷贝

二、生成子字符串

  1. a.子符串范围:beginIdex-->最后
  2. public String substring(int beginIndex) {
  3. if (beginIndex < 0) {
  4. throw new StringIndexOutOfBoundsException(beginIndex);
  5. }
  6. int subLen = value.length - beginIndex;
  7. if (subLen < 0) {
  8. throw new StringIndexOutOfBoundsException(subLen);
  9. }
  10. return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
  11. }
  12. b.子符串范围:beginIdex-->endIndex
  13. public String substring(int beginIndex, int endIndex) {
  14. if (beginIndex < 0) {
  15. throw new StringIndexOutOfBoundsException(beginIndex);
  16. }
  17. if (endIndex > value.length) {
  18. throw new StringIndexOutOfBoundsException(endIndex);
  19. }
  20. int subLen = endIndex - beginIndex;
  21. if (subLen < 0) {
  22. throw new StringIndexOutOfBoundsException(subLen);
  23. }
  24. return ((beginIndex == 0) && (endIndex == value.length)) ? this
  25. : new String(value, beginIndex, subLen);
  26. }

三、替换

字符替换,replace(char oldChar, char nerChar)
字符串替换,replaceFirst(String regex, String replacement)
                      replaceFirst(String regex, String replacement)

四、切割

  1. public String[] split(String regex, int limit) {
  2. /* fastpath if the regex is a
  3. (1)one-char String and this character is not one of the
  4. RegEx's meta characters ".$|()[{^?*+\\", or
  5. (2)two-char String and the first char is the backslash and
  6. the second is not the ascii digit or ascii letter.
  7. */
  8. char ch = 0;
  9. if (((regex.value.length == 1 &&
  10. ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
  11. (regex.length() == 2 &&
  12. regex.charAt(0) == '\\' &&
  13. (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
  14. ((ch-'a')|('z'-ch)) < 0 &&
  15. ((ch-'A')|('Z'-ch)) < 0)) &&
  16. (ch < Character.MIN_HIGH_SURROGATE ||
  17. ch > Character.MAX_LOW_SURROGATE))
  18. {
  19. int off = 0;
  20. int next = 0;
  21. boolean limited = limit > 0;
  22. ArrayList<String> list = new ArrayList<>();
  23. while ((next = indexOf(ch, off)) != -1) {
  24. if (!limited || list.size() < limit - 1) {
  25. list.add(substring(off, next));
  26. off = next + 1;
  27. } else { // last one
  28. //assert (list.size() == limit - 1);
  29. list.add(substring(off, value.length));
  30. off = value.length;
  31. break;
  32. }
  33. }
  34. // If no match was found, return this
  35. if (off == 0)
  36. return new String[]{this};
  37. // Add remaining segment
  38. if (!limited || list.size() < limit)
  39. list.add(substring(off, value.length));
  40. // Construct result
  41. int resultSize = list.size();
  42. if (limit == 0)
  43. while (resultSize > 0 && list.get(resultSize - 1).length() == 0)
  44. resultSize--;
  45. String[] result = new String[resultSize];
  46. return list.subList(0, resultSize).toArray(result);
  47. }
  48. return Pattern.compile(regex).split(this, limit);
  49. }


java的String类(一)的更多相关文章

  1. Java中String类的方法及说明

    String : 字符串类型 一.      String sc_sub = new String(c,3,2);    //      String sb_copy = new String(sb) ...

  2. java.lang.String 类的所有方法

    java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...

  3. java中String类学习

    java中String类的相关操作如下: (1)初始化:例如,String s = “abc”; (2)length:返回字符串的长度. (3)charAT:字符操作,按照索引值获得字符串中的指定字符 ...

  4. 深入分析Java的String类的方法与特点

    字符串是任何编程语言都必须支持的变量类型,有些编程语言是直接提供了原生的变量类型,有些编程语言则使用语法特性以 SDK 的形式提供支持.在Java编程平台中,对字符串的支持使用了后者的形式,就是通过在 ...

  5. java 中String类的常用方法总结,带你玩转String类。

    String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...

  6. 在java中String类为什么要设计成final

    在java中String类为什么要设计成final? - 胖胖的回答 - 知乎 https://www.zhihu.com/question/31345592/answer/114126087

  7. java中String类为什么不可变?

    在面试中经常遇到这样的问题:1.什么是不可变对象.不可变对象有什么好处.在什么情景下使用它,或者更具体一点,java的String类为什么要设置成不可变类型? 1.不可变对象,顾名思义就是创建后的对象 ...

  8. JAVA的String类的常用方法(转载)

    Java-String类的常用方法总结   一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的f ...

  9. 【转载】Java中String类的方法及说明

    转载自:http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html String : 字符串类型 一.      String sc_ ...

  10. 在java中String类为什么要设计成final?

    大神链接:在java中String类为什么要设计成final? - 程序员 - 知乎 我进行了重新排版,并且更换了其中的一个例子,让我们更好理解. String很多实用的特性,比如说“不可变性”,是工 ...

随机推荐

  1. oracle分析函数 之分组累加求和

    select s.slice_date_to ,s.made_id ,sum(s.steup_count)over(partition by s.made_id order by s.slice_da ...

  2. 08 Linux下MySQL的下载、安装及启动

    测试环境 主机系统:Win7 64位 虚拟机:VMware® Workstation 11.1.0 虚拟机系统:CentOS 6.5 64位   Kernel 2.6.32-431.e16.x86_6 ...

  3. sql在添加新列时同时指定default约束名称

    alter table tab_testadd col_test NOT NULL CONSTRAINT DFtab_test_col_test DEFAULT 1 引用:http://www.uzz ...

  4. SPOJ #691. Hotel Floors

    A typical flood-fill algorithm application (BFS). Not very complex, except only 1 tip: instead of se ...

  5. (转)[SQL Server] 动态sql给变量赋值(或返回值给变量)

    本文转载自:http://blog.csdn.net/xiaoxu0123/article/details/5684680 [SQL Server] 动态sql给变量赋值(或返回值给变量) decla ...

  6. Python Beautiful Soup模块的安装

    以安装Beautifulsoup4为例: 1.到网站上下载:http://www.crummy.com/software/BeautifulSoup/bs4/download/ 2.解压文件到C:\P ...

  7. Metro 应用无法打开解决办法

    1.恢复默认UAC设置: 2.恢复自带防火墙服务启动类型为自动: 3.设置IE为默认浏览器(搜索和应用中): 4.如有可能使用Microsoft帐户,而不是本地帐户(仅使用有限的几个Metro应用): ...

  8. [转]将Word转(保存)为带书签的PDF

    提到的方法非常管用,感谢原作者的分享. 原文地址:http://blog.163.com/rongting_chen/blog/static/16490684420114266192887/ 将wor ...

  9. Educational Codeforces Round 15 Cellular Network

    Cellular Network 题意: 给n个城市,m个加油站,要让m个加油站都覆盖n个城市,求最小的加油范围r是多少. 题解: 枚举每个城市,二分查找最近的加油站,每次更新答案即可,注意二分的时候 ...

  10. 转:谷歌大脑科学家 Caffe缔造者 贾扬清 微信讲座完整版

    [转:http://blog.csdn.net/buaalei/article/details/46344675] 大家好!我是贾扬清,目前在Google Brain,今天有幸受雷鸣师兄邀请来和大家聊 ...