String类常用方法的实现:
常用方法 | 功能说明 |
---|---|
public int length() | 返回字符串的长度 |
puclic boolean equals(Object anObject) | 将给定字符串与当前字符串比较,若两字符串相等,则返回true,否则返回false |
public String substring(int beginIndex) | 返回字符串中从beginIndex开始到字符串末尾的子串 |
public String substring(int beginIndex,int endIndex) | 返回begin到begin-1的子串 |
public int charAt(int index) | 返回index指定位置的字符 |
public int indexOf(String str) | 返回str在字符串中第一次出现的位置 |
public int compareTo(String anotherString) | 若调用该方法的字符串大于参数字符串返回大于0的值;若等于则返回数0;若小于参数字符串,则返回小于0值 |
public String replace(char OldString,char newString) | 以newChar字符替换字符串中所有oldChar字符 |
public String trim() | 去掉字符串的首尾字符空格 |
public String toLowerCase() | 将字符串的所有字符都转换成小写字符 |
public String toUpperCase() | 将字符串的所有字符都转换成大写字符 |
下面对每个方法进行实现:
package Example;
//public int length()方法的实现
public class Example4 {
public static void main(String[] args)
{
String str=new String("abc");
System.out.print(str.length());
}
}
运行结果:
3
package Example;
//puclic boolean equals(Object anObject)
public class Example4 {
public static void main(String[] args)
{
String str1=new String("abc");
String str2=new String("def");
String str3=new String("cba");
System.out.println(str1.equals(str2));//false
System.out.println(str1.equals(str3));//false
}
}
运行结果:
false
false
package Example;
//public String substring(int beginIndex)的方法实现
public class Example4 {
public static void main(String[] args)
{
String str1=new String("abc");
str1=str1.substring(1);
System.out.println(str1);
}
}
运行结果:
bc
package Example;
//public String substring(int beginIndex,int endIndex)的实现
public class Example4 {
public static void main(String[] args)
{
String str1=new String("abcef");
str1=str1.substring(1,4);
System.out.println(str1);
}
}
运行结果:
bce
package Example;
//public int charAt(int index)的实现
public class Example4 {
public static void main(String[] args)
{
String str1=new String("abcef");
System.out.println(str1.charAt(2));
}
}
运行结果:
c
package Example;
//public int indexOf(String str)的实现
public class Example4 {
public static void main(String[] args)
{
String str1=new String("abcefe");
System.out.println(str1.indexOf("e"));
}
}
运行结果:
3
package Example;
//public int compareTo(String anotherString)的实现
public class Example4 {
public static void main(String[] args)
{
String str1=new String("abcef");
System.out.println(str1.compareTo("e"));
}
}
运行结果:
-4
package Example;
//public String replace(char OldString,char newString)的实现
public class Example4 {
public static void main(String[] args)
{
String str1=new String("abcefe");
System.out.println(str1.replace('e','g'));
}
}
运行结果:
abcgfg
package Example;
//public String trim()的实现
public class Example4 {
public static void main(String[] args)
{
String str1=new String(" ab cefe ");
System.out.println("去首尾空格前:"+str1);
System.out.println("去首尾空格后:"+str1.trim());
}
}
运行结果:
去首尾空格前: ab cefe
去首尾空格后:ab cefe
package Example;
//public String toLowerCase()与public String toUpperCase()的实现
public class Example4 {
public static void main(String[] args)
{
String str1=new String("AbCefE");
System.out.println("输出所有字符:"+str1);
System.out.println("所有字符变小写:"+str1.toLowerCase());
System.out.println("所有字符变大写:"+str1.toUpperCase());
}
}
运行结果:
输出所有字符:AbCefE
所有字符变小写:abcefe
所有字符变大写:ABCEFE
上面主要实现了String类的使用方法,需注意的是String类对象创建之后不会再做修改和变动,上述例子中有:
String str1=new String(“abc”);
str1=str1.substring(1);
实际的存储是换了内存的,如下面的代码所示:
package Example;
public class Example4 {
public static void main(String[] args)
{
String str1=new String("abc");
System.out.println("输出String str1的存储地址:"+str1.hashCode());
str1=str1.substring(1);
System.out.println("输出str1=str1.substring(1):"+str1);
System.out.println("输出str1=str1.substring(1)的存储地址:"+str1.hashCode());
}
}
运行结果:
输出String str1的存储地址:96354
输出str1=str1.substring(1):bc
输出str1=str1.substring(1)的存储地址:3137
当然,还有变动的StringBuilder类和StringBuffer类:
StringBuffer:用于多任务并发,这种情况下需要同步以防止StringBuffer崩溃。
StringBuilder:单任务访问更有效。
基本方法可以点击访问菜鸟教程