jdk String类源码解析
一、基本使用
1.1 matches函数匹配正则表达式
import java.util.*;public class test{public static void main(String[] args) throws InterruptedException {String baseChange="100000";System.out.println(baseChange.matches("^10*$"));}}
一、属性
1.1 常用属性
String的底层是使用char数组来实现的
//底层char数组
private final char value[];//hash是String实例化的hashcode的一个缓存。因为String经常被用于比较,比如在HashMap中。如果每次进行比较都重新计算Hashcode的话,那是比较麻烦的,所以保存一个hashcode的缓存能优化这样的操作
private int hash; // Default to 0
二、方法
2.1 构造函数
默认构造函数,创建一个新的空字符序列,因为String本身是一个不可变序列,因此不建议使用这个构造函数
public String() {this.value = "".value;}
将传入的String做一个拷贝。
public String(String original) {this.value = original.value;this.hash = original.hash;}
将字符序列的值拷贝进入String中,后续对字符序列的修改不会改变String的值
public String(char value[]) {this.value = Arrays.copyOf(value, value.length);}
截取数组的某一部分,作为传入String的拷贝,同样对原数组的修改不会影响到String的值
public String(char value[], int offset, int count) {if (offset < 0) {throw new StringIndexOutOfBoundsException(offset);}if (count <= 0) {if (count < 0) {throw new StringIndexOutOfBoundsException(count);}if (offset <= value.length) {this.value = "".value;return;}}// Note: offset or count might be near -1>>>1.if (offset > value.length - count) {throw new StringIndexOutOfBoundsException(offset + count);}this.value = Arrays.copyOfRange(value, offset, offset+count);}
2.2 数组相关常用函数
//返回字符数组的长度
public int length() {return value.length;
}//判断字符数组长度是否为0
public boolean isEmpty() {return value.length == 0;
}//返回在字符数组特定位置的字符串值
public char charAt(int index) {if ((index < 0) || (index >= value.length)) {throw new StringIndexOutOfBoundsException(index);}return value[index];
}
2.2 substring
/*** Returns a string that is a substring of this string. The* substring begins with the character at the specified index and* extends to the end of this string. <p>* Examples:* <blockquote><pre>* "unhappy".substring(2) returns "happy"* "Harbison".substring(3) returns "bison"* "emptiness".substring(9) returns "" (an empty string)* </pre></blockquote>** @param beginIndex the beginning index, inclusive.* @return the specified substring.* @exception IndexOutOfBoundsException if* {@code beginIndex} is negative or larger than the* length of this {@code String} object.*/public String substring(int beginIndex) {if (beginIndex < 0) {throw new StringIndexOutOfBoundsException(beginIndex);}int subLen = value.length - beginIndex;if (subLen < 0) {throw new StringIndexOutOfBoundsException(subLen);}return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);}
/*** Returns a string that is a substring of this string. The* substring begins at the specified {@code beginIndex} and* extends to the character at index {@code endIndex - 1}.* Thus the length of the substring is {@code endIndex-beginIndex}.* <p>* Examples:* <blockquote><pre>* "hamburger".substring(4, 8) returns "urge"* "smiles".substring(1, 5) returns "mile"* </pre></blockquote>** @param beginIndex the beginning index, inclusive.* @param endIndex the ending index, exclusive.* @return the specified substring.* @exception IndexOutOfBoundsException if the* {@code beginIndex} is negative, or* {@code endIndex} is larger than the length of* this {@code String} object, or* {@code beginIndex} is larger than* {@code endIndex}.*/public String substring(int beginIndex, int endIndex) {if (beginIndex < 0) {throw new StringIndexOutOfBoundsException(beginIndex);}if (endIndex > value.length) {throw new StringIndexOutOfBoundsException(endIndex);}int subLen = endIndex - beginIndex;if (subLen < 0) {throw new StringIndexOutOfBoundsException(subLen);}return ((beginIndex == 0) && (endIndex == value.length)) ? this: new String(value, beginIndex, subLen);}
2.3 compareTo
/*** Compares two strings lexicographically.* The comparison is based on the Unicode value of each character in* the strings. The character sequence represented by this* {@code String} object is compared lexicographically to the* character sequence represented by the argument string. The result is* a negative integer if this {@code String} object* lexicographically precedes the argument string. The result is a* positive integer if this {@code String} object lexicographically* follows the argument string. The result is zero if the strings* are equal; {@code compareTo} returns {@code 0} exactly when* the {@link #equals(Object)} method would return {@code true}.* <p>* This is the definition of lexicographic ordering. If two strings are* different, then either they have different characters at some index* that is a valid index for both strings, or their lengths are different,* or both. If they have different characters at one or more index* positions, let <i>k</i> be the smallest such index; then the string* whose character at position <i>k</i> has the smaller value, as* determined by using the < operator, lexicographically precedes the* other string. In this case, {@code compareTo} returns the* difference of the two character values at position {@code k} in* the two string -- that is, the value:* <blockquote><pre>* this.charAt(k)-anotherString.charAt(k)* </pre></blockquote>* If there is no index position at which they differ, then the shorter* string lexicographically precedes the longer string. In this case,* {@code compareTo} returns the difference of the lengths of the* strings -- that is, the value:* <blockquote><pre>* this.length()-anotherString.length()* </pre></blockquote>** @param anotherString the {@code String} to be compared.* @return the value {@code 0} if the argument string is equal to* this string; a value less than {@code 0} if this string* is lexicographically less than the string argument; and a* value greater than {@code 0} if this string is* lexicographically greater than the string argument.*/public int compareTo(String anotherString) {int len1 = value.length;int len2 = anotherString.value.length;int lim = Math.min(len1, len2);char v1[] = value;char v2[] = anotherString.value;int k = 0;while (k < lim) {char c1 = v1[k];char c2 = v2[k];if (c1 != c2) {return c1 - c2;}k++;}return len1 - len2;}
2.4 toCharArray
/*** Converts this string to a new character array.** @return a newly allocated character array whose length is the length* of this string and whose contents are initialized to contain* the character sequence represented by this string.*/public char[] toCharArray() {// Cannot use Arrays.copyOf because of class initialization order issueschar result[] = new char[value.length];System.arraycopy(value, 0, result, 0, value.length);return result;}
2.5 valueOf
/*** Returns the string representation of the {@code double} argument.* <p>* The representation is exactly the one returned by the* {@code Double.toString} method of one argument.** @param d a {@code double}.* @return a string representation of the {@code double} argument.* @see java.lang.Double#toString(double)*/public static String valueOf(double d) {return Double.toString(d);}
2.6 copyValueOf
/*** Equivalent to {@link #valueOf(char[], int, int)}.** @param data the character array.* @param offset initial offset of the subarray.* @param count length of the subarray.* @return a {@code String} that contains the characters of the* specified subarray of the character array.* @exception IndexOutOfBoundsException if {@code offset} is* negative, or {@code count} is negative, or* {@code offset+count} is larger than* {@code data.length}.*/public static String copyValueOf(char data[], int offset, int count) {return new String(data, offset, count);}
/*** Equivalent to {@link #valueOf(char[])}.** @param data the character array.* @return a {@code String} that contains the characters of the* character array.*/public static String copyValueOf(char data[]) {return new String(data);}
2.7 format
/*** Returns a formatted string using the specified format string and* arguments.** <p> The locale always used is the one returned by {@link* java.util.Locale#getDefault() Locale.getDefault()}.** @param format* A <a href="../util/Formatter.html#syntax">format string</a>** @param args* Arguments referenced by the format specifiers in the format* string. If there are more arguments than format specifiers, the* extra arguments are ignored. The number of arguments is* variable and may be zero. The maximum number of arguments is* limited by the maximum dimension of a Java array as defined by* <cite>The Java™ Virtual Machine Specification</cite>.* The behaviour on a* {@code null} argument depends on the <a* href="../util/Formatter.html#syntax">conversion</a>.** @throws java.util.IllegalFormatException* If a format string contains an illegal syntax, a format* specifier that is incompatible with the given arguments,* insufficient arguments given the format string, or other* illegal conditions. For specification of all possible* formatting errors, see the <a* href="../util/Formatter.html#detail">Details</a> section of the* formatter class specification.** @return A formatted string** @see java.util.Formatter* @since 1.5*/public static String format(String format, Object... args) {return new Formatter().format(format, args).toString();}
2.8 indexOf
返回特定字符在这个String中第一次出现的下标,如果字符的大小位于 0 to 0xFFFF之间,那么会返回满足this.charAt(k) = = ch的最小的k,否则,返回满足this.codePointAt(k) == ch最小的k。如果不存在这样的ch,返回-1。这里的ch是Unicode
编码的。
public int indexOf(int ch) {return indexOf(ch, 0);}
上面的函数是从String中char数组的下标0开始搜索的,下面的这个函数自己指定了下标
public int indexOf(int ch, int fromIndex) {final int max = value.length;if (fromIndex < 0) {fromIndex = 0;} else if (fromIndex >= max) {// Note: fromIndex might be near -1>>>1.return -1;}if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {// handle most cases here (ch is a BMP code point or a// negative value (invalid code point))final char[] value = this.value;for (int i = fromIndex; i < max; i++) {if (value[i] == ch) {return i;}}return -1;} else {return indexOfSupplementary(ch, fromIndex);}}
2.9 startsWith
检验以某个特定下标开始的原字符串的一个子串是否以特定的前缀(传入的参数)作为开端。
public boolean startsWith(String prefix, int toffset) {char ta[] = value;int to = toffset;char pa[] = prefix.value;int po = 0;int pc = prefix.value.length;// Note: toffset might be near -1>>>1.if ((toffset < 0) || (toffset > value.length - pc)) {return false;}while (--pc >= 0) {if (ta[to++] != pa[po++]) {return false;}}return true;}
检测这个字符串是不是以特定的前缀(作为参数传入的字符串)为开端。当传入的参数为空或者等于原字符串时同样会返回true
public boolean startsWith(String prefix) {return startsWith(prefix, 0);}
split
/*** Splits this string around matches of the given* <a href="../util/regex/Pattern.html#sum">regular expression</a>.** <p> The array returned by this method contains each substring of this* string that is terminated by another substring that matches the given* expression or is terminated by the end of the string. The substrings in* the array are in the order in which they occur in this string. If the* expression does not match any part of the input then the resulting array* has just one element, namely this string.** <p> When there is a positive-width match at the beginning of this* string then an empty leading substring is included at the beginning* of the resulting array. A zero-width match at the beginning however* never produces such empty leading substring.** <p> The {@code limit} parameter controls the number of times the* pattern is applied and therefore affects the length of the resulting* array. If the limit <i>n</i> is greater than zero then the pattern* will be applied at most <i>n</i> - 1 times, the array's* length will be no greater than <i>n</i>, and the array's last entry* will contain all input beyond the last matched delimiter. If <i>n</i>* is non-positive then the pattern will be applied as many times as* possible and the array can have any length. If <i>n</i> is zero then* the pattern will be applied as many times as possible, the array can* have any length, and trailing empty strings will be discarded.** <p> The string {@code "boo:and:foo"}, for example, yields the* following results with these parameters:** <blockquote><table cellpadding=1 cellspacing=0 summary="Split example showing regex, limit, and result">* <tr>* <th>Regex</th>* <th>Limit</th>* <th>Result</th>* </tr>* <tr><td align=center>:</td>* <td align=center>2</td>* <td>{@code { "boo", "and:foo" }}</td></tr>* <tr><td align=center>:</td>* <td align=center>5</td>* <td>{@code { "boo", "and", "foo" }}</td></tr>* <tr><td align=center>:</td>* <td align=center>-2</td>* <td>{@code { "boo", "and", "foo" }}</td></tr>* <tr><td align=center>o</td>* <td align=center>5</td>* <td>{@code { "b", "", ":and:f", "", "" }}</td></tr>* <tr><td align=center>o</td>* <td align=center>-2</td>* <td>{@code { "b", "", ":and:f", "", "" }}</td></tr>* <tr><td align=center>o</td>* <td align=center>0</td>* <td>{@code { "b", "", ":and:f" }}</td></tr>* </table></blockquote>** <p> An invocation of this method of the form* <i>str.</i>{@code split(}<i>regex</i>{@code ,} <i>n</i>{@code )}* yields the same result as the expression** <blockquote>* <code>* {@link java.util.regex.Pattern}.{@link* java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link* java.util.regex.Pattern#split(java.lang.CharSequence,int) split}(<i>str</i>, <i>n</i>)* </code>* </blockquote>*** @param regex* the delimiting regular expression** @param limit* the result threshold, as described above** @return the array of strings computed by splitting this string* around matches of the given regular expression** @throws PatternSyntaxException* if the regular expression's syntax is invalid** @see java.util.regex.Pattern** @since 1.4* @spec JSR-51*/public String[] split(String regex, int limit) {/* fastpath if the regex is a(1)one-char String and this character is not one of theRegEx's meta characters ".$|()[{^?*+\\", or(2)two-char String and the first char is the backslash andthe second is not the ascii digit or ascii letter.*/char ch = 0;if (((regex.value.length == 1 &&".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||(regex.length() == 2 &®ex.charAt(0) == '\\' &&(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&((ch-'a')|('z'-ch)) < 0 &&((ch-'A')|('Z'-ch)) < 0)) &&(ch < Character.MIN_HIGH_SURROGATE ||ch > Character.MAX_LOW_SURROGATE)){int off = 0;int next = 0;boolean limited = limit > 0;ArrayList<String> list = new ArrayList<>();while ((next = indexOf(ch, off)) != -1) {if (!limited || list.size() < limit - 1) {list.add(substring(off, next));off = next + 1;} else { // last one//assert (list.size() == limit - 1);list.add(substring(off, value.length));off = value.length;break;}}// If no match was found, return thisif (off == 0)return new String[]{this};// Add remaining segmentif (!limited || list.size() < limit)list.add(substring(off, value.length));// Construct resultint resultSize = list.size();if (limit == 0) {while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {resultSize--;}}String[] result = new String[resultSize];return list.subList(0, resultSize).toArray(result);}return Pattern.compile(regex).split(this, limit);}
equals
两个指向不同对象的引用,只要这两个对象的字符数组是相同的,那么就会返回true
与==的对比:
对于引用类型,==号比较的是两个对象的地址值是否一样,即两个引用指向的是否是同一个对象。
public boolean equals(Object anObject) {if (this == anObject) {return true;}if (anObject instanceof String) {String anotherString = (String)anObject;int n = value.length;if (n == anotherString.value.length) {char v1[] = value;char v2[] = anotherString.value;int i = 0;while (n-- != 0) {if (v1[i] != v2[i])return false;i++;}return true;}}return false;}
参考
jdk String类源码解析相关推荐
- Java String类源码解析
String直接继承Object 含有一个char[] value,还有一个int hash默认值为0 new String()的构造产生的是一个值为""的字符数组 String( ...
- Java String类源码阅读笔记
文章目录 一.前置 二.String类源码解析 1.String类继承关系 2.成员变量 3.构造方法 4.长度/判空 5.取字符 6.比较 7.包含 8.hashCode 9.查询索引 10.获取子 ...
- java的String类源码详解
java的String类源码详解 类的定义 public final class Stringimplements java.io.Serializable, Comparable<String ...
- java.lang 源码剖析_java.lang.Void类源码解析
在一次源码查看ThreadGroup的时候,看到一段代码,为以下: /* * @throws NullPointerException if the parent argument is {@code ...
- Node 学习六、核心模块 events之 01 events 和 EventEmitter 类、发布订阅、EventEmitter 类源码解析和模拟实现
events 事件模块 events 与 EventEmitter node.js 是基于事件驱动的异步操作架构,内置 events 模块 events 模块提供了 EventEmitter 类 这个 ...
- Scroller类源码解析及其应用(一)
滑动是我们在自定义控件时候经常遇见的难题,让新手们倍感困惑,这篇文章主要介绍Scroller类的源码,告诉打击这个到底有什么用,怎么使用它来控制滑动.另外,我还会结合一个简单的例子,来看一下这个类的应 ...
- Java FileReader InputStreamReader类源码解析
FileReader 前面介绍FileInputStream的时候提到过,它是从文件读取字节,如果要从文件读取字符的话可以使用FileReader.FileReader是可以便利读取字符文件的类,构造 ...
- Unsafe类源码解析
前言 Unsafe,顾名思义,一个不安全的类,那么jdk的开发者为什么要设计一个不安全的类呢?这个类为什么会不安全呢?现在就让我们来揭开Unsafe类的神秘面纱. 1.概述 作为java开发者的我们都 ...
- java String部分源码解析
String类型的成员变量 /** String的属性值 */ private final char value[];/** The offset is the first index of the ...
- Java集合---Arrays类源码解析
一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类型: ...
最新文章
- Docker 安装registry (构建私有镜像库)
- [译] Robinhood 为什么使用 Airflow
- scapy-yield的含义和使用
- D2前端技术论坛之网页无障碍学习笔记
- 10.原码 反码 补码
- 【Spring注解系列06】FactoryBean注入对象用法
- STL算法中函数对象和谓词
- horizon client 无法识别域_LY-W100摄像头视频定时拍照图像识别分析抄表读表无线远传水表数_水表吧...
- odis工程师使用方法_傅里叶红外光谱仪常见故障维修及排除方法,你了解有多少种呢?...
- Lines(HDU-5124)
- wps表格数字和名字分开_WPS表格怎么把一串数字用句号分?
- 康奈尔大学研究员发现“代码投毒”攻击,可触发供应链攻击
- iOS.UIKit.07.UIAlertView_UIActionSheet
- 基于c语言的成绩管理系统,基于C语言实现学生成绩管理系统.docx
- AdTime:多屏时代下传统媒体的鼓起
- C++析构函数定义和使用
- win7 U盘安装和激活
- Android 系统简单介绍
- 从零开始搭建个人静态简历网站
- juniper SRX55 简单配置
热门文章
- 自定义RatingBar
- kubernetes集群Pod详细信息为Failed create pod sandbox,缺失镜像google_containers/pause-amd64.3.0解决方法
- 为什么大家都说 SELECT * 效率低
- Android支付实践(二)之微信支付详解与Demo
- python基础篇——异常
- 3 缓存文件写入失败_分布式缓存数据库一致性问题
- xadmin获取mysql_Django2集成xadmin详解-5-获取登录用户信息并填充相应Model字段
- java http 工具类_Java发送Http请求工具类
- 算法知识点——(1)特征工程
- anaconda flaks 安装_Anaconda 安装和配置