String类的其他功能

String类的其他功能

package com.heima.string;

public class Demo07_StringMethod {

	/**
	 * * A:String的替换功能及案例演示
			* String replace(char old,char new)
			* String replace(String old,String new)
		* B:String的去除字符串两空格及案例演示
			* String trim()
		* C:String的按字典顺序比较两个字符串及案例演示
			* int compareTo(String str)(暂时不用掌握)
			* int compareToIgnoreCase(String str)(了解)
	 */
	public static void main(String[] args) {
		//demo01();
		//demo02();
		//java的编码是Unicode码表值
		//字节转化成字符串,字符串转化成字节肯定是JBK的
		String s1 = "a";
		String s2 = "aaaa";
		
		int num = s1.compareTo(s2);				//按照码表值比较
		System.out.println(num);
		
		String s3 = "黑";
		String s4 = "马";
		int num2 = s3.compareTo(s4);
		System.out.println('黑' + 0);			//查找的是unicode码表值
		System.out.println('马' + 0);
		System.out.println(num2);
		
		String s5 = "heima";
		String s6 = "HEIMA";
		int num3 = s5.compareTo(s6);
		System.out.println(num3);
		
		int num4 = s5.compareToIgnoreCase(s6);
		System.out.println(num4);
		
		/*
		 * public int compare(String s1, String s2) {
            int n1 = s1.length();
            int n2 = s2.length();
            int min = Math.min(n1, n2);
            for (int i = 0; i < min; i++) {
                char c1 = s1.charAt(i);
                char c2 = s2.charAt(i);
                if (c1 != c2) {
                    c1 = Character.toUpperCase(c1);						//将c1字符转换成大写
                    c2 = Character.toUpperCase(c2);						//将c2字符转换成大写
                    if (c1 != c2) {
                        c1 = Character.toLowerCase(c1);					//将c1字符转换成小写
                        c2 = Character.toLowerCase(c2);					//将c2字符转换成小写
                        if (c1 != c2) {
                            // No overflow because of numeric promotion
                            return c1 - c2;
                        }
                    }
                }
            }
            return n1 - n2;
		 */
	}

	private static void demo02() {
		String s = "   hei   ma   ";
		String s2 = s.trim();
		System.out.println(s2);
	}

	private static void demo01() {
		String s = "heima";
		String s2 = s.replace('i', 'o');			//用o替换i
		System.out.println(s2);
		
		String s3 = s.replace('z', 'o');			//z不存在,保留原字符不改变
		System.out.println(s3);
		
		String s4 = s.replace("ei", "ao");
		System.out.println(s4);
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

左绍骏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值