Java——String类常用方法的实现

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:单任务访问更有效。
基本方法可以点击访问菜鸟教程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值