Sstring类

import java.io.*;
class Noname22
{
	//java对直接使用字符串类型做了优化,当两个变量值相同时,第二变量不再新开辟空间
	public static void main(String[] args) 
	{
		String a = "我想当个好人";
		String b = "我想当个好人";
		if(a == b){
		  System.out.println("地址相同");
		}else{
          System.out.println("地址不相同");		
		}
		String c = new String ("三年又三年");
		String d = new String ("三年又三年");
		if (c==d)
		{
			System.out.println("地址相同");
		}else{
		    System.out.println("地址不相同");
		}
		if (c.equals(d))
		{
			System.out.println("内容相同");
		}else{
            System.out.println("内容不相同");		
		}
		String data = "我爱何宛橙";
		System.out.println(data.charAt(1));

		String email = "qq.haoren@.com";
		if (email.matches("\\w+.\\w+@+.\\w+"))
		{
          System.out.println("邮箱通过");
		}else{
		  System.out.println("邮箱不通过");
		}
        String username = "haoren213";
		if (username.matches("\\w{6,18}"))
		{
			System.out.println("用户通过");
		}else{
		    System.out.println("用户不通过");
		}
		String tel = "17628073917";
		if (tel.matches("^1\\d{10}"))
		{
      System.out.println("号码通过");
		}else{
		System.out.println("号码不通过");
		}
		String arrs = "www.haoren.com";
		String[] s = arrs.split("[.]");
		for (String x:s )
		{
			System.out.println(x);
		}
		//方法一

		String path = "C:\\Windows\\System32\\cmd.exe";
        String path1 = "c:"+File.separator+"Windows"+File.separator+"System32"+File.separator+"cmd.exe";
		System.out.println(path1);
		String[] paths = path.split("\\\\");
		System.out.println(paths[3]);
         //方法二

		 int index = path.lastIndexOf("\\");//这里的参数不是正则表达
		String temp = path.substring(index+1);
		System.out.println(temp);
		//判读字符串的开头和结尾

		String cyr = "我爱你何宛橙";
		System.out.println(cyr.startsWith("我"));	
		System.out.println(cyr.endsWith("橙"));
		//String 方法太慢不建议
        String data1 = "我";
		 data1 =data1+ "爱你";
		System.out.println(data1);
		//如果需要反复拼接字符串,用StringBuffer
        StringBuffer sb = new StringBuffer ("我");
		sb.append ("爱你");
		System.out.println(sb);
        //去掉空格
		//trim()只会去掉两边
		String urse = "   何宛橙    ";
		System.out.println("   何宛橙    ");
		System.out.println(urse.trim());
		//替换 replace
		String data3 = "何宛橙爱你";
		System.out.println(data3.replace("你","我"));
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值