java.lang.String分析摘要:
<1>join重载方法
<2>toLowerCase重载方法
<3>toUpperCase重载方法
<4>trim()方法
<5>toString()方法
1.join重载方法
该方法根据this字符串适配和regex匹配返回一个boolean值。
public static String join(CharSequence delimiter, CharSequence... elements){
//该方法如果参数不为null,则返回参数本身,否则抛出NullPointerException异常
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
StringJoiner joiner = new StringJoiner(delimiter);
for(CharSequence cs : elements){
joiner.add(cs);
}
return joiner.toString();
}
public static String join(CharSequence delimiter,
Interable<? extends CharSequence> elements){
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
StringJoiner joiner = new StringJoiner(delimiter);
for(CharSequence cs : elements){
join.add(cs);
}
return joiner.toString();
}
这个方法是的属性有:public公有的、static静态的。
参数:delimiter字符序列,String也属于字符序列
参数:【Object…】elements字符序列组,用于依次拼接在delimter结尾
参数:【Interable <? extends CharSequence>】elements实现Interator的对象
返回值:String类型
返回值说明:返回将elements中的字符序列拼接在delimiter的String对象。
该方法注意点:
实现Interable的类可以调用Interator()方法。
2. toLowerCase重载方法
该方法的作用是将字符全部转换成小写。
public String toLowerCase(){
return toLowerCase(Locale.getDefault());
}
public String toLowerCase(Locale locale){
if(locale == null){
throw new NullPointerExcetpion();
}
int firstUpper;
final int len = value.length;
//检查是否需要本身就是大写,如果是的话就不需要转
scan:{
for(firstUpper = 0; firstUpper<len;){
char c = value[firstUpper];
if((c>=Character.MIN_HIGH_SURROGATE)
&& (C<=Character.toLowerCase(Character.MAX_HIGH_SURROGATE))){
int supplChar = codePointAt(firstUpper);
if(supplChar != Character.toLowerCase(supplChar)){
break scan;
}
//确定表示指定字符(Unicode代码点)所需的char值的数目。
//如果指定字符等于或大于0x10000,则该方法返回2。否则,该方法返回1。
firstUpper += Character.charCount(supplChar);
}else{
if(c != Character.toLowerCase(c)){
break scan;
}
firstUpper++;
}
}
return this;
}
char[] result = new char[len];
int resultOffset = 0;
//结果可能会增长,因此I+Rebug偏移是结果中的写入位置。
System.arraycopy(value, 0, result, 0, firstUpper);
String lang = locale.getLanguage();
boolean localeDependent =
(lang=="tr" || lang=="az" || lang=="lt");
char[] lowerCharArray;
int lowerChar;
int srcChar;
int srcCount;
for(int i=firstUpper; i<len; i+=srcCount){
srcChar = (int)value[i];
if((char)srcChar >= Character.MIN_HIGH_SURROGATE
&& (char)srcChar <= Character.MAX_HIGH_SURROGATE){
srcCount = codePointAt(i);
srcCount = Character.charCount(srcChar);
}eles{
srcCount = 1;
}
if(localeDependent ||
srcChar=='\u03A3' || //希腊大写字母西格玛
srcChar == '\u0130') {//拉丁文大写字母I
lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
}else{
lowerChar = Character.toLowerCase(srcChar);
}
if((lowerChar==Character.ERROR)
|| (lowerChar>=Character.MIN_SUPPLEMENTARY_POINT)){
if(lowerChar == Character.ERROR){
lowerCharArray = ConditionalSpecialCasing.toLowerCaseCharArray(
this, i, locale);
}else if(srcCount == 2){
resultOffset += Character.toChars(lowerChar, result,
i+resultOffset) - srcCount;
continue;
}else{
lowerCharArray = Character.toChars(lowerChar);
}
int maplen = lowerCharArray.length;
if(mapLen > srcCount){
char[] result2 = new char[result.length + maplen -srcCount];
System.arraycopy(result, 0, result2, 0, i + resultOffset);
result = result2;
}
for(int x=0; x<mapLen;++x){
result[i+resultOffset+x] = lowerCharArray[x];
}
resultOffset += (mapLen - srcCount);
}esle{
result[i+resultOffset] = (char)lowerChar;
}
}
return new String(result, 0, len+resultOffset);
}
这个方法是的属性有:public公有。
参数:locale国际化对象,里面存储了一些国际化的信息,比如语言方式等
返回值:String类型
返回值说明:返回该字符串的小写字符序列。
该源码的思路是:
<1>如果本身字母就都是小写的话,返回this字符串。
<2>如果不是则返回小写字符序列
3.toUpperCase重载方法
该方法会返回this字符序列转成大写的形式。
public String toUpperCase(){
return toUpper(Locale.getDefault());
}
public String toUpperCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstLower;
final int len = value.length;
/* Now check if there are any characters that need to be changed. */
scan: {
for (firstLower = 0 ; firstLower < len; ) {
int c = (int)value[firstLower];
int srcCount;
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
c = codePointAt(firstLower);
srcCount = Character.charCount(c);
} else {
srcCount = 1;
}
int upperCaseChar = Character.toUpperCaseEx(c);
if ((upperCaseChar == Character.ERROR)
|| (c != upperCaseChar)) {
break scan;
}
firstLower += srcCount;
}
return this;
}
/* result may grow, so i+resultOffset is the write location in result */
int resultOffset = 0;
char[] result = new char[len]; /* may grow */
/* Just copy the first few upperCase characters. */
System.arraycopy(value, 0, result, 0, firstLower);
String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] upperCharArray;
int upperChar;
int srcChar;
int srcCount;
for (int i = firstLower; i < len; i += srcCount) {
srcChar = (int)value[i];
if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
(char)srcChar <= Character.MAX_HIGH_SURROGATE) {
srcChar = codePointAt(i);
srcCount = Character.charCount(srcChar);
} else {
srcCount = 1;
}
if (localeDependent) {
upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
} else {
upperChar = Character.toUpperCaseEx(srcChar);
}
if ((upperChar == Character.ERROR)
|| (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (upperChar == Character.ERROR) {
if (localeDependent) {
upperCharArray =
ConditionalSpecialCasing.toUpperCaseCharArray(
this, i, locale);
} else {
upperCharArray = Character.toUpperCaseCharArray(srcChar);
}
} else if (srcCount == 2) {
resultOffset += Character.toChars(upperChar, result,
i + resultOffset) - srcCount;
continue;
} else {
upperCharArray = Character.toChars(upperChar);
}
/* Grow result if needed */
int mapLen = upperCharArray.length;
if (mapLen > srcCount) {
char[] result2 = new char[result.length + mapLen - srcCount];
System.arraycopy(result, 0, result2, 0, i + resultOffset);
result = result2;
}
for (int x = 0; x < mapLen; ++x) {
result[i + resultOffset + x] = upperCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)upperChar;
}
}
return new String(result, 0, len + resultOffset);
}
这个方法是的属性有:public公有。
参数:locale国际化对象,里面存储了一些国际化的信息,比如语言方式等
返回值:String类型
返回值说明:返回该字符串的大写字符序列。
该源码的思路是:
<1>如果本身字母就都是大写的话,返回this字符串。
<2>如果不是则返回大写字符序列
4. trim()方法
该方法的作用将this字符串中首尾的空字符串除去,然后将其返回。
public String trim(){
int len = value.length;
int st = 0;
char[] val = value;
while((st<len) && (val[st]<='')){
st++;
}
while((st<len) && (val[len-1]<='')){
len--;
}
return ((st>0)||(len<value.length))?substring(st,len):this;
}
这个方法是的属性有:public公有。
返回值:String类型
返回值说明:返回该字符串除去首尾空字符的字符串。
该源码思路:
<1>用一个st记录this字符串开始有几个空字符。
<2>用一个len记录this字符串结尾处往回数有几个空字符。
<3>如果st或len有一个大于0,则切割字符串,返回首尾除去空字符的字符串。
<4>否则返回this对象。
5. toString()方法
返回this对象,即字符串本身的字符。
public String toString(){
return this;
}
这个方法是的属性有:public公有。
返回值:String字符串
返回值说明:返回this对象。