javase基础篇学习之String常用方法

package String类;

public class StringTest {

	public static void main(String[] args) {
		//不可变字符串意思就是会生成一个新的字符串  不能再原来基础上直接使用
		String  s = new String("abcdefg");
		String s2 = "abcdefg";
		String s3 = "ABCDEFG";
		System.out.println(s.equals(s2));
		//忽略大小写判断字符串是否相等
		System.out.println(s.equalsIgnoreCase(s3));
		//length()返回当前字符串对象的长度
		System.out.println(s.length());
		//返回字符串对象中的某个下标的单个字符
		char c_a=s.charAt(0);
		char c_b=s.charAt((s.length()-1));
		System.out.println(c_a);
		System.out.println(c_b);
		//从头开始找返回所包含的第一个子字符串的索引
		int index=s.indexOf("c");
		System.out.println(index);
		//从最后开始找所包含的第一个子字符串的索引
		int lastIndex=s.indexOf("c");
		System.out.println(lastIndex);
		//字符串替换 
		String s4=s2.replace("a", "A");
		System.out.println(s4);
		String s5=s2.replace("ab", "cd");
		System.out.println(s5);
		//开头或者结尾字符串判断
		//判断是否为ab开头的
		System.out.println(s.startsWith("ab"));
		//判断是否为ee开头的
		System.out.println(s.endsWith("ee"));
		//截取字符串,注意:截取字符串包头不包尾
		String s6 = "abcdefgh".substring(2);
		System.out.println(s6);
		String s7 = "abcdefgh".substring(2,6);
		System.out.println(s7);
		//大小写转换
		System.out.println("abcde".toUpperCase());//转成大写
		System.out.println("Abced".toLowerCase());//转成小写
		//删除字符串头部和尾部的空格
		String s8="  ab  ";
		String s9=s8.trim();
		System.out.println(s9);
		//类型转换
		String s10 = "123456";
		System.out.println(Integer.valueOf(s10));
		
	}

}
待续补充
运行结果:

true
true
7
a
g
2
2
Abcdefg
cdcdefg
true
false
cdefgh
cdef
ABCDE
abced
ab
123456


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值