zoukankan      html  css  js  c++  java
  • 字符串比较——compareTo函数

    package com.day03.ifelse;
    
    import java.math.BigInteger;
    
    /**
     * @author 王恒
     * @datetime 2017年4月7日 下午3:13:50
     * @description 字符串比较
     */
    public class TestCompareTo {
    
    	public static void main(String[] args) {
    		//单个英文字母的字符串比较
    		String str = "a";
    		String str2 = "b";
    		System.out.println(str.compareTo(str2)+"   单个英文字母");
    		
    		//多个英文字母的比较
    		String str3 = "aasf";
    		String str4 = "gasfd";
    		System.out.println(str3.compareTo(str4)+"   多个英文字母
    ");
    
    		 //单个的中文的字符串比较
    		 char c1='上';
    		 char c2='海';
                     //new BigInteger(Integer.toHexString(s),16);//将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。
    		 System.out.println(new BigInteger(Integer.toHexString(c1),16)+"   上");//19978
    		 System.out.println(new BigInteger(Integer.toHexString(c2),16)+"   海");//28023
    
    		String s1 = "上";
    		String s2 = "海";
    		System.out.println(s1.compareTo(s2)+"    单个的中文的字符串比较"); //-8045
    		
    		//多个的中文字符串比较
    		String s3 = "上战场";
    		String s4 = "上";
    		System.out.println("多个中文比较       "+s3.compareTo(s4)+"      多个的中文字符串比较");
    		
    	}
    }
    

      

  • 相关阅读:
    shell中十种实现自加的方法
    expect 安装使用
    wireshark常用过滤规则
    linux错误收集
    18.socket概述
    17.异常处理/模块与包
    15.常用模块【time/os/sys】
    14.继承与授权
    13.面向对象(多态/(性)/封装)
    11.高阶函数(匿名/*递归/函数式)对象编程基础
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/6679409.html
Copyright © 2011-2022 走看看