(JAVA)String类之比较方法(2)

String类:
获取字符串的内容
一、获取字符串长度
    public int length() {
        return value.length;
    }
    length  与 lenght()的区别:
    前面是属性,不需要传参数
    后面是方法
二、指定下角标,返回固定位置的对应字符串

public char charAt(int index) {
        if ((index < 0) || (index >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
        }
三、返回一个字符在字符串中第一次出现的位置
public int indexOf(String str) {
        return indexOf(str, 0);
    }
四、返回一个字符在指定位置后,出现的位置
    public int indexOf(String str, int fromIndex) {
        return indexOf(value, 0, value.length,
                str.value, 0, str.value.length, fromIndex);
    }
五、返回一个字符(字符串)在另一个字符串中最后一次出现的位置
    public int lastIndexOf(String str) {
        return lastIndexOf(str, value.length);
    }
六、获取字符串的一部分
String substring(int beginIndex,int endIndex);
    public String substring(int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        if (endIndex > value.length) {
            throw new StringIndexOutOfBoundsException(endIndex);
        }
        int subLen = endIndex - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return ((beginIndex == 0) && (endIndex == value.length)) ? this
                : new String(value, beginIndex, subLen);
    }
七、转换
1.将字符串转换为字节数组
    public byte[] getBytes(Charset charset) {
        if (charset == null) throw new NullPointerException();
        return StringCoding.encode(charset, value, 0, value.length);
    }
2.将字符串全部转成小写
 public String toLowerCase(Locale locale) {
        if (locale == null) {
            throw new NullPointerException();
        }

        int firstUpper;
        final int len = value.length;

    Now check if there are any characters that need to be changed.
        scan: {
                for (firstUpper = 0 ; firstUpper < len; ) {
        char c = value[firstUpper];
        if ((c >= Character.MIN_HIGH_SURROGATE)
        && (c <= Character.MAX_HIGH_SURROGATE)) {
        int supplChar = codePointAt(firstUpper);
        if (supplChar != Character.toLowerCase(supplChar)) {
        break scan;
        }
        firstUpper += Character.charCount(supplChar);
        } else {
        if (c != Character.toLowerCase(c)) {
        break scan;
        }
        firstUpper++;
        }
        }
        return this;
        }
 3.将字符串全部转为大写
 String.toUpperCase(Locale.ROOT);
 4.切割,将字符串切割为指定要求的字符串数组,参数为正则表达式
 String[] split
 5,替换String replase(char oldchar.char newchar);
 String replase(String oldString.String newString);
 6.去掉字符串中两端空格去掉:String trim()
 7.两个字符串的字典形式比较
 int compareTo(String s)

 
import java.nio.charset.StandardCharsets;
import java.util.Locale;

public class getStringDome {
    public static void main(String[] args){
        methon_1();
        methon_2();
        methon_3();
        menth_4();
        methon_5();
    }
    public static void methon_1(){
         String a = "asdfhgvscasdf";
         System.out.println(a.length());
        System.out.println(a.charAt(3));

        System.out.println(a.indexOf("s"));
        System.out.println(a.indexOf("s",2));
        System.out.println(a.lastIndexOf("s"));
        System.out.println(a.lastIndexOf("s",4));
        System.out.println(a.substring(2,5));
        //遍历字符串
//        for (int x = 0;x<a.length();x++){
//            System.out.println(a.charAt(x));
//        }
    }
    public static void methon_2(){
        byte[]  bytes="张三".getBytes(StandardCharsets.UTF_8);
        for (int x = 0;x < bytes.length;x++){
        System.out.println(bytes[x]);}
        byte[] c = {-27,-68,-96};
        String m = new String(c);
        System.out.println(m);
    }
    public static void methon_3(){
        String s1 = "AJDSKVvdknvv";
        s1 = s1.toLowerCase(Locale.ROOT);
        System.out.println(s1);
        String s2;
        s2 = s1.toUpperCase(Locale.ROOT);
        System.out.println(s2);
    }
    public static void menth_4(){
        String a ="asdkvjbwiejfhljzcnvndj";
        String[] a1 = a.split("j");
        for (int x = 0;x <a1.length;x++){
        System.out.println(a1[x]);}
        System.out.println("==========================");

        String b = "192.168.0.1";
        String[] b1 =b.split("\\.");
        for (int x = 0;x <a1.length;x++){
        System.out.println(b1[x]);}
        System.out.println("==========================");
    }
    public static void methon_5(){
        String c = "zxcvb" ;
        String c1 = "asdf";
        int c2 = c.compareTo(c1);
        System.out.println(c2);
    }

总结

1. Scanner 了解接收键盘输入
   以后使用IO接收  Scanner(System.in)

2. 字符串 -- 熟练
 2.1 定义方式,两种
   |-- 直接定义 String s = ""
   |-- new方法  String s = new String(""),不推荐使用 
   |-- 定义方法区别,前者一个对象,后者两个对象

 2.2 字符串的底层结构--特性
   |-- private  final char[] value 字符串就是一个字符数组
   |-- String字符串,是一个常量,不可以改变
   |-- 字符串面试题。引用类型中,数组,类类型,接口,都会改变,唯一就String不变

 2.3 构造器
   |-- 字节数组变成字符串, new 传递字节数组,查询编码表 byte[] = {};
   |-- 字符数组变成字符串, new 传递字符数组,不查询编码表 char[] = {}

 
 2.4 判断方法
   |-- equals 重写Object方法,比较字符串是不是完全相等
   |-- equalsIgnoreCase 比较字符串是不是完全相等,忽略大小写
   |-- contains 判断一个字符串是不是包含另一个字符串
   |-- isEmpty 判断是不是空串
   |-- startsWith 判断一个字符串是不是以另一个字符串开头
   |-- endsWith 判断一个字符串是不是以另一个字符串结尾

 2.5 获取方法
   |-- charAt 指定索引返回单个字符
   |-- indexOf 指定字符,返回第一次出现的索引
   |-- lastIndexOf 指定字符,返回最后一次出现的索引
   |-- length 返回字符串中字符个数
   |-- substring 获取字符串一部分,包含头,不包含尾

 2.6 转换方法
   |-- getBytes 查询编码表,将字符串转成字节数组
   |-- toCharArray 不查询编码表,将字符串转成字符数组
   |-- toUpperCase 字符串转成大写
   |-- toLowerCase 字符串转成小写

 2.7 其他方法
   |-- split 切割字符串
   |-- replace 替换字符串
   |-- trim 去掉两端空格
   |-- compareTo 字典顺序比较字符串,又成为自然顺序

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值