网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
当String str1 = new String();//就是声请长度为1的字符数组;
—————————————————————————————————————————————————————————————
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
//这个构造方法就是可以传进字符串进来。
String str2 = new String(“sssss”);//可以传入字符串进来。
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
//构造方法,传递一个数组,但是这里有数组的copy。需要把这个数组Arrany.copyOf到我们这个类的数组里面。
char[] c = {‘a’, ‘v’};
String str3 = new String©;
//传递一个数组进来,在这个构造方法里面调用了Array.copyOF(),
//Array类中public static char[] copyOf(char[] original, int newLength)进行赋值
}
public int length() {
return value.length;
}
//length()这个方法就是把这个数组的长度返回来了。
public boolean isEmpty() {
return value.length == 0;
}
//如果长度为0那么就是空嘛。
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
// charAt,就是返回索引指向的字符,从0开始;
//注意中间还穿插了一个如果索引不在范围之内就会抛出一个异常,
String strr = “abcder”;
System.out.println(strr.charAt(2));//返回的是c
关于public boolean equals(Object anObject) 方法
![这里写图片描述](https://img-blog.csdn.net/20161031140253135)
String strr1 = new String("sjjdsf");
String strr2 = strr1;
System.out.println(strr2.equals(strr2));
//返回tur,因为指向同一个对象。直接在第一部分就是返回ture了。
--------------------------------------------------------
String strr1 = new String("sjjdsf");
String strr2 = new String ("sjjdsf");
System.out.println(strr1.equals(strr2));//比较的是内容是否相等。
System.out.println(strr1==strr2);//比较的是他们引用是否相等
//顺便回忆一下,equal方法默认方法是与==一致的,因此一般都是需要重写的。
String str = "absd";
String str1 = "absd";
System.out.println(str.equals(str1));//肯定是ture
//这个应该也是true;”abcd“双引号括起来的字符串是表示在常量池中。
***来个内存图的分析;***
String str = new String(“abcd”);
String str2 = new String(“abcd”);
String str3 = “def”;
String str4 = “def”;
![这里写图片描述](https://img-blog.csdn.net/20161031142654442)
indexOf()方法
Returns the index within this string of the first occurrence of
* the specified character.
返回索引,在这个字符串中第一次出现的这个字符。
public int indexOf(int ch) {//调用重载方法
return indexOf(ch, 0);//0;表示这个字符串,从最开头开始找。
}
重载方法;
public int indexOf(int ch, int fromIndex) {//formIndex开始找,
String str = new String(“abcd”);
System.out.println(str.indexOf(‘b’));//返回索引位置,并且注意索引都是从0开始的;
System.out.println(str.indexOf(‘e’));//如果没有找到那么就返回-1;
lastIndextOF()//从后面开始找。
System.out.println(“Abcdb”.indexOf(‘d’));//3
System.out.println(“Abcdb”.lastIndexOf(‘b’));//4
public String substring(int beginIndex)//截取字符串的方法。返回时字符串对象;
- Returns a new 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.
//返回一个子字符串对象,并且子字符串的对象从传人的索引这个值所值的字符开始截取,一直到字符串结尾。
//如果传人的值为负数,则会抛出异常。
public String substring(int beginIndex, int endIndex) //一个重载方法;
//表示的是从beginInder到endIndex的截取。
String str = new String(“abcd”);
String str1 = str.substring(2);
System.out.println(str1);//返回cd
- Returns a new string resulting from replacing all occurrences of
oldChar
in this string withnewChar
.
//返回一个新的字符串对象,从把字符串中的newchar用oldchar代替的新字符串
If the characteroldChar
does not occur in the- character sequence represented by this
String
object, - then a reference to this
String
object is returned.
//如果这个老的字符没有出现在字符串里面,那么就会返回原字符串对象的引用。、return this。
String str = new String(“abcd”);
String str1 = str.replace(‘a’, '');
System.out.println(str1);//返回bcd
![这里写图片描述](https://img-blog.csdn.net/20161031150358302)
public String[] split(String regex) //对字符串进行切割,返回一个数组。
String str = “abcd,abc,dmhjkl,”;
String[] strArray = str.split(“,”);//按逗号进行切割。
for(int i=0;i<strArray.length;i++){
System.out.println(strArray[i]);
}
public String trim() //去除首尾空格。
String str = " assdf aw da f ";
String str1 = str.trim();//去除首尾空格;
System.out.println(str1);//返回assdf aw da f
public char[] toCharArray()
//就是将String对象中的数组返回,返回的是一个新的数组,这个数组就可以修改了。
public boolean equalsIgnoreCase(String anotherString)//忽略大小写进行比较
Compares this {@code String} to another {@code String}, ignoring case
- considerations.
System.out.println(“ASs”.equalsIgnoreCase(“asS”));//返回true
public boolean startsWith(String prefix)//表示字符串是否以这个开始。
public boolean endsWith(String suffix)//表示字符串是否以这个结尾。
System.out.println("Abcdb".startsWith("Ab"));//ture
System.out.println("Abcdb".endsWith("db"));//true;
public String toLowerCase() //把字符串中的字母全变成小写
public String toUpperCase()//把字符串的字母全变为大写;
System.out.println(“ADs”.toLowerCase());//ads
System.out.println(“ADS”.toUpperCase());//ADS
**常见的拼字符串代码,但是并不好,很浪费空间。因为要声明很多对象\*\***
String gh = “g”;//这句话就是有两个对象,“g”也单独算一个对象/
for(int i = 0; i < 10; i++){//这里会创建9个对象。一起就是11个对象(常做面试题)
gh += i;//相当于,gh = gh + i;
}
System.out.println(gh);//g0123456789
//要注意,这里的gh是变化的,为什么;因为String不可变,是指的”g“里面的,
//如果改为final String gh = “g”; 那就的确是不可变的但是String里面的因此gh是可变的。
![这里写图片描述](https://img-blog.csdn.net/20161031155941325)
再强调一下,private final char value[];是引用的值不可变,但是value里面的元素是可以变的,但是String类是private并且也没有暴露可以修改的接口,因此value里面的值还是不能修改。(封装)
对于源码的学习,要适当,看自己知道的就ok,了解这个类大概有什么方法。
**附上上方各种方法的代码**
package textString;
import javax.swing.plaf.synth.SynthSeparatorUI;
public class Stringtext {
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
733324)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!