java---String类的解析和用法与StringBuffer的用法

1、String类的构造

public class string_text1{
	public static void main(String[] args) {
		String s1, s2, s3, s4;              //声明string对象
		char[] a = {'s', 't', 'u', 'd', 'e', 'n', 't'};  
		s1 = new String("we are students");	//创建string对象
		s2 = "student";
		s3 = "student";
		s4 = "students";
		String s5 = new String(a, 3, 4);    //从数组a的第3位下标开始截取,共截取4位字符
		System.out.println(s1);
		System.out.println(s2 == s3);
		System.out.println(s2 == s4);
		System.out.println(s5);
	}
}

运行结果:
we are students
true
false
dent

2、String类的常用方法
(方法:
.length()、.equals()、.equalsIgnoreCase()、.startsWith()、.endWith()、.compareTo()、.contains()、.indexOf()、.substring()、.trim())

public class string_text2 {
	public static void main(String[] args) {
		String s1 = "我们是学生";
		String s2 = new String("we are students");
		String s3 = new String(" you are students ");
		String s4 = new String("We are students");
		String s6 = new String("we are students");
		System.out.println(s1.length());					//求s1的长度
		System.out.println(s2.equals(s4));					//比较s2与s4参数是否相等(区分大小写)
		System.out.println(s2.equals(s6));					//比较s2与s6参数是否相等
		System.out.println(s2.equalsIgnoreCase(s4));		//比较s2与s4参数是否相等(不区分大小写)
		System.out.println(s1.startsWith("我们"));			//判断s1是否以“我们”开头
		System.out.println(s1.startsWith("你们"));			//判断s1是否以“你们”开头
		System.out.println(s1.endsWith("学生"));			//判断s1是否以“学生”结尾
		System.out.println(s1.endsWith("老师"));			//判断s1是否以“老师”结尾
		System.out.println(s3.compareTo("you are"));		//按字典序进行比较,大于返回正数,小于返回
															//负数,等于返回0
		System.out.println(s3.compareTo(" you are student and teacher "));
		System.out.println(s3.compareTo(" you are students "));
		System.out.println(s3.contains("are"));				//判断s3中是否含有“are”“your”
		System.out.println(s3.contains("your"));
		System.out.println(s3.indexOf("we"));				//返回we第一次出现的下标
		System.out.println(s3.indexOf("you"));
		System.out.println(s3.indexOf("s",8));				//返回s第一次出现的下标,从第8位下标开始
		System.out.println(s3.substring(9, 16));			//从s3的第9位开始截取,到16位结束
		System.out.println(s3.trim());						//去除s3前后的空格
	}
}

运行结果:
5
false
true
true
true
false
true
false
-89
83
0
true
false
-1
1
9
student
you are students

3、String对象基本数据与转换
(方法:Integer.parseInt()、String.valueOf())

public class string_text3{
	public static void main(String[] args) {
		String s1 = "123456789";
		int x1 = Integer.parseInt(s1);			//s1转换为int型
		System.out.println(getType(s1));
		System.out.println(getType(x1));
		int x2 = 123456789;
		String s2 = String.valueOf(x2);			//x2转换为String型
		System.out.println(getType(s2));
		System.out.println(getType(x2));
}
	/*判断数据类型方法*/
		public static String getType(Object o){ //获取变量类型方法
			return o.getClass().toString(); //使用int类型的getClass()方法
	} 
}

运行结果:
class java.lang.String
class java.lang.Integer
class java.lang.String
class java.lang.Integer

4、字符序列与字节数组
(方法:getChars()、toCharArray())

public class string_text4 {
	public static void main(String[] args) {
		char[] a, b;
		String s = "德国足球队击败巴西队";
		a = new char[2];
		s.getChars(5, 7, a, 0);		//从s的第5位开始截取到第7位。将这些数从a的第0位开始依次复制。
		System.out.println(a);
		b = "大家好,很高兴认识大家".toCharArray();//转化位char类型
		System.out.println(b);
	}
}

运行结果:
击败
大家好,很高兴认识大家

5、StringBuffer的用法
(方法:setCharAt()、append()、insert()、replace())

public class stringBuffer{
	public static void main(String args[]){
		String str1 = " 你好哦";
		StringBuffer str2 = new StringBuffer("Hello Worlds");
		str2.setCharAt(11, '!');			//将字符str第11个位置字符替换成s
		System.out.println(str2);
		str2.append(str1);					//在字符串后面进行字符串的添加
		System.out.println(str2);
		str2.insert(6, "my ");				//在字符串的第6位添加后面的字符串
		System.out.println(str2);
		int index = str2.indexOf("World!");//寻找字符串World!的下标
		str2.replace(index, index + 6, "dog");//从起始位置到结束位置替换为dog
		System.out.println(str2);
	}
}

运行结果:
Hello World!
Hello World! 你好哦
Hello my World! 你好哦
Hello my dog 你好哦

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值