String字符串处理和常用API

字符串常用API

public class Hello{
	
	// String 的常用API : 每一个都要自己动手写一下
	public static void main(String[] _args){
		
		String word = "hello world web";
		
		// charAt(int index)  //获取字符串单个字符
		System.out.println(word.charAt(0));
		int len = word.length();
		System.out.println(word.charAt(len-1));
		
		// compareTo(String anotherString)  字符串比较大小
		// 先比较字符 unicode 码大的字符串比较大
		// 如果unicode 码比较都相同 则比较字符串的长度 长的大
		// 如果相等则返回 0
		System.out.println(word.compareTo("hello world"));
		
		// concat(String str) //字符串拼接
		String concat = word.concat(" web");
		System.out.println(concat);
		
		// contains(CharSequence s)  判断一个字符串是否包含另外一个字符串
		System.out.println(word.contains("hello"));
		
		// endsWith(String suffix)  判断一个字符串的后缀 可以用于判断文件类型
		System.out.println("music.mp3".endsWith(".mp4"));
		
		// equals(Object anObject) 比较2个字符串对象 ******	
		// 如果字符串是以字面量的方式声明 则可以使用 == 比较 
		String a = "abc";
		String b = "abc";
		System.out.println(a==b);// true
		// 如果使用的是new 的方式则不能使用 == 比较
		String c = new String("abc");
		System.out.println(a==c);// false
		
		System.out.println(a.equals(c));
		
		// format(String format, Object... args) 
		String name = "jack";
		int age = 20;
		double height = 1.888; // %m.nf  m表示宽度 n表示小数 整数=m-n
		System.out.printf("姓名是%s,年龄%d,身高%.2f\n",name,age,height);
		System.out.println(String.format("姓名是%s,年龄%d,身高%.2f",name,age,height));
		
		// indexOf(String str)  获取指定字符串 在当前字符串的位置
		System.out.println(word.indexOf("world"));
		
		
		// replace(CharSequence target, CharSequence replacement)  替换
		String replaceWord = word.replace("world","web");
		System.out.println(replaceWord);
		
		// split() 字符串切割 成一个字符串数组
		String[] _words = word.split(" ");
		for(String w : _words){
			System.out.println(w);
		}
		
		// substring(int beginIndex)  截取字符串
		// url : https://www.google.com/search?q=电脑&oq=电脑&aqs=chrome..69i57j69i60l4.1046j0j8&sourceid=chrome&ie=UTF-8
		// 获取用户的搜索内容
		String url = "https://www.google.com/search?q=电脑&oq=电脑&aqs=chrome..69i57j69i60l4.1046j0j8&sourceid=chrome&ie=UTF-8";
		
		// 获取 ? 所在的index
		int index = url.indexOf("?");
		// 从index 开始截取
		String argStr = url.substring(index+1);
		// 以&进行split
		String[] args = argStr.split("&");
		// 以 = 进行split 如果 word[0].equals("q");
		for(String arg:args){
			String[] words = arg.split("=");
			if(words[0].equals("q")){
				System.out.println(words[1]);
			}
		}
		
		// toCharArray() 
		char[] cs = word.toCharArray();
		
		for(char cword : cs){
			System.out.println(cword);
		}
		
		// toLowerCase() 转小写
		// toUpperCase() 转大写
		// trim() 去掉空格    [   hello   ]  =>[hello]
		// valueOf()  可以将任意数据类型转成字符串
		
		boolean flag = true;
		// System.out.println(flag+100);
		System.out.println(String.valueOf(flag)+100);
	}
	
}

可以参考:http://c.biancheng.net/java/40/ 练习

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值