JAVA基础——API(String)

//String类的常用方法

//简化输出格式
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}


		String s1="abcbde";
		sop(s1.length()); 
		sop(s1.charAt(3)); 
		sop(s1.indexOf('b'));
		sop(s1.indexOf('b',2));
		sop(s1.indexOf("bc"));
		sop(s1.lastIndexOf('b'));

		String s="abcde";
		sop(s.contains("bcd"));
		sop(s.startsWith("abc"));
		sop(s.endsWith("de"));
		String s1="abCde";
		sop(s.equals(s1));
		sop(s.equalsIgnoreCase(s1)); //忽略大小写



		char[] ch={'a','b','c','d','e'};
		//char->string
		//构造方法
		String s=new String(ch);
		String s1=new String(ch,2,2);//第一个2是索引,第二个2是个数
		sop(s);
		sop(s1);
		//静态方法
		String s2=String.copyValueOf(ch);
		String s3=String.copyValueOf(ch,2,2);
		sop(s2);
		sop(s3);
		//string->char
		String s4="abcdef";
		char[] ch1=s4.toCharArray();
		for (int x=0;x<ch1.length ;x++ )
		{
			sop(ch1[x]);
		}
		//将字节数组转成字符串。
			//String(byte[])
			//String(byte[],offset,count):将字节数组中的一部分转成字符串。
		//将字符串转成字节数组。
			//byte[]  getBytes():
		//将基本数据类型转成字符串。
			//static String valueOf(int)
			//static String valueOf(double)

			3+"";//String.valueOf(3);

		//特殊:字符串和字节数组在转换过程中,是可以指定编码表的。


		String s="haha java";
		sop(s.replace('a','o'));
		sop(s.replace("java","haha"));

		String s = "wohao haha hss";

		String[] arr  = s.split(" ");

		for(int x = 0; x<arr.length; x++)
		{
			sop(arr[x]);
		}


		String s="abcde";
		sop(s.substring(1));  //等价于sop(s.substring(1,s.length()));
		sop(s.substring(1,3));//取到头,取不到尾:bc


		String s="   ab C de   ";
		sop(s.toUpperCase());
		sop(s.toLowerCase());
		sop(s.trim());		//去掉两端所有空格
		String s1="abc";
		String s2="aaa";
		sop(s1.compareTo(s2));




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值