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. }


最新文章

  1. 【转】Vim十大必备插件
  2. linux修改open files数
  3. Sublime Text 2 配置及其使用
  4. iOS 5.0 后UIViewController新增:willMoveToParentViewController和didMoveToParentViewCon
  5. swift UIAlertController教程
  6. POJ3107--Godfather(树的重心)
  7. Python 学习入门(22)—— 线程同步
  8. 201521123060 《Java程序设计》第9周学习总结
  9. centos6 升级pip后导致pip不可用
  10. Mac之日常操作
  11. (二分查找 拓展) leetcode 69. Sqrt(x)
  12. umask 文件默认权限
  13. How to create ISO on macOS
  14. 2017中国无人机公开赛 总决赛 CDR Final 竞赛规则
  15. JavaScript JSON对象(一)
  16. Shell变量while循环内改变无法传递到循环外
  17. Vue 框架中遇到的诀窍
  18. unity, texture import settings
  19. Winter-2-STL-D The Blocks Problem 解题报告及测试数据
  20. vs2013 快捷键

热门文章

  1. shopex 网店系统安装教程
  2. 【extjs】 ext5 Ext.grid.Panel 分页,搜索
  3. C/C++ 函数压栈方式
  4. jsonp的简单例子
  5. TextBox仿Foxmail收件人删除效果
  6. 75. Sort Colors
  7. 49. Group Anagrams
  8. Mysql存储过程简明使用
  9. MySql5.6 Window超详细安装教程
  10. [Android NDK/JNI-1A]-开发环境搭建