7月25日上午笔记StringAPI的详细功能学习

String

判断

boolean equals(Object obj) //用来比较字符串的内容
boolean equalsIgnoreCase(String str) //忽略字符串大小写比较字符串内容
boolean contains(String str) //判断当前字符串对象是否包含,目标字符串的字符序列
boolean startsWith(String str) //判断当前字符串对象,是否已目标字符串的字符序列开头
boolean endsWith(String str)// 判断当前字符串,是否以目标字符串对象的字符序列结尾
boolean isEmpty() 判断一个字符串,是不是空字符串

  public static void main(String[] args) {
     // boolean equals(Object obj)  //用来比较字符串的内容
   String s1 = new String("hEllo");
   String s2 = "hello";
   //System.out.println(s1.equals(s2));
   //boolean equalsIgnoreCase(String str) //忽略字符串大小写比较字符串内容
   //System.out.println(s1.equalsIgnoreCase(s2));


 String s3 = "zhangsan";
   String s4 = "ang";
   // boolean contains(String str) //判断当前字符串对象是否包含,目标字符串的字符序列
   boolean contains = s3.contains(s4);
   //System.out.println(contains);

  //boolean startsWith(String str) //判断当前字符串对象,是否已目标字符串的字符序列开头
   boolean b = s3.startsWith(s4);
   boolean zh = s3.startsWith("zh");
   //System.out.println(b);
   //System.out.println(zh);

  // boolean endsWith(String str)// 判断当前字符串,是否以目标字符串对象的字符序列结尾
   // String name = "a.txt";  name.endsWith(".txt")
   boolean san = s3.endsWith("san");
   boolean b1 = s3.endsWith(s4);
   //System.out.println(san);
   //System.out.println(b1);

  //boolean isEmpty()
   String s = new String();
   boolean empty = s.isEmpty();
   System.out.println(empty);

  boolean empty1 = "".isEmpty();
   System.out.println(empty1);
 }

  、、、public boolean isEmptyStr(String s) {
    return s == null || s.isEmpty();
    }

获取

  • // 获取当前字符串对象中,包含的字符个数
    int  length()
    
  • // 获取字符串对象代表字符序列中,指定位置的字符
    char charAt(int index)

  • // 在当前字符串对象中,查找指定的字符,如果找到
    // 就返回字符,首次出现的位置,如果没找到返回-1
    int indexOf(int ch)

  • // 查找当前字符串中,目标字符串首次出现的位置(如果包含)
    // 找不到,返回-1, 这里的位置是指目标字符串的第一个字符
    // 在当前字符串对象中的位置
    int indexOf(String str)

  • // 指定,从当前字符串对象的指定位置开始,查找首次出现的指定字符的位置(如果没找到返回-1)
    int indexOf(int ch,int fromIndex)

  • // 指定,从当前字符串对象的指定位置开始,查找首次出现的指定字符串的位置(如果没找到返回-1)
    int indexOf(String str,int fromIndex)

  • // 返回字符串,该字符串只包含当前字符串中,从指定位置开始(包含指定位置字符),的那部分字符串(直到字符串末尾)
    // “zhangsan” -> “san”
    String substring(int start)

  • // 返回字符串,只包含当前字符串中,从start位置开始(包含),到end(不包含)指定的位置,
    // 这部分字符串 [start, end) -> [start, end - 1]
    String substring(int start,int end)

public static void main(String[] args) {

    String s1 = "zhangsan";
    //int length()
    int length = s1.length();
    //System.out.println(length);

    //char charAt(int index) 获取字符串对象代表字符序列中,指定位置的字符
    char c = s1.charAt(4);
    //System.out.println(c);

    //int indexOf(int ch)

    // 找到就返回首次出现的位置
    int aIndex = s1.indexOf('a');
    // 找不到就返回-1
    int wIndex = s1.indexOf('w');
    //System.out.println(aIndex);
    //System.out.println(wIndex);

    //int indexOf(String str)
    String s2 = "an";
    int anIndex = s1.indexOf(s2);
    System.out.println(anIndex);
    int leeIndex = s1.indexOf("lee");
    System.out.println(leeIndex);


    // 指定,从当前字符串对象的指定位置开始,查找首次出现的指定字符的位置(如果没找到返回-1)
    //int indexOf(int ch,int fromIndex)

    int aIndex2 = s1.indexOf('a', 3);
    //System.out.println(aIndex2);

    //int indexOf(String str,int fromIndex)
    int anIndex2 = s1.indexOf("an", 3);
    //System.out.println(anIndex2);

    //String substring(int start)
    String sub1 = s1.substring(5);
    //System.out.println(sub1);
    //System.out.println(s1);

    //String substring(int start,int end)
    String sub2 = s1.substring(2, 5); //[2, 5)
    System.out.println(sub2);

  }

转换

byte[] getBytes() // 获取一个用来表示字符串对象字符序列的,字节数组
char[] toCharArray() // 获取的是用来表示字符串对象字符序列的,字符数组

static String valueOf(char[] chs)
static String valueOf(int i)

String toLowerCase() //把字符串全部转化为小写
String toUpperCase() // 把字符串全部装华为大写

String concat(String str) //字符串拼接 作用等价于 + 实现的字符串拼接

public static void main(String[] args) {

    //byte[] getBytes()
    //byte[] getBytes()  // 获取一个用来表示字符串对象字符序列的,字节数组
    String s = "abcde";
    byte[] bytes = s.getBytes();
    System.out.println(Arrays.toString(bytes));

    //   char[] toCharArray() // 获取的是用来表示字符串对象字符序列的,字符数组
    char[] chars = s.toCharArray();
    System.out.println(Arrays.toString(chars));

    // static String valueOf(char[] chs)
   

    //static String valueOf(int i)
    int i = 10;
    String s2 = String.valueOf(i);
    System.out.println(s2); // 10 -> "10"

    String s3 = "zhanGSan";
    //String toLowerCase() //把字符串全部转化为小写
    String s4 = s3.toLowerCase();
    System.out.println(s4);

    //String toUpperCase() // 把字符串全部装华为大写
    String s5 = s3.toUpperCase();
    System.out.println(s5);

    // String concat(String str) //字符串拼接 作用等价于 + 实现的字符串拼接
    String s6 = "hello,";
    String s7 = "world";
    String concat = s6.concat(s7);
    System.out.println(concat);
    System.out.println(s6 + s7);
  }

替换

String类的替换功能
String replace(char old,char new) // 在新的字符串中,用新(new)字符,替换旧(old)字符
String replace(String old, String new) //在新的字符串中,用新的字符串(new), 替换旧(old)字符串

String类 去除空字符
String trim() //在新的字符串中,去掉开头和结尾的空格字符

String类的比较功能
int compareTo(String str)
int compareToIgnoreCase(String str)

  1. 字符串的大小如何比较?
    按照字典序,比较字符串的大小。字典序原本的含义实质,英文单词在字典中出现的先后顺序
    (在字典中,先出现的字符串小,后出现的字符串大)

    具体到编程语言,是根据两个字符串字符串从左往右数,第一个对应位置的不同字符,来决定两字符串的大小
    hellofasfsaf
    headf

    shift按两次 搜索某个类
    ctrl + f12

    可以通过定义一个接口,定义一种规则, 协议。
    interface Comparable {
    int compareTo(String obj)
    }

    比较:
    大于: compareTo方法的返回值 正数 > 0 当前字符串对象 > 待比较的参数字符串对象
    小于: compareTo方法的返回值 负数 < 0 当前字符串对象 < 待比较的参数字符串对象
    等于: compareTo方法的返回值 = 0 当前字符串对象 = 待比较的参数字符串对象

 public static void main(String[] args) {

    String s1 = "zhangsan";
    //String replace(char old,char new) // 在新的字符串中,用新(new)字符,替换旧(old)字符
    String replace = s1.replace('a', 'w');
    //System.out.println(replace);
    //System.out.println(s1);


    // String replace(String old, String new) //在新的字符串中,用新的字符串(new), 替换旧(old)字符串
    String replace1 = s1.replace("an", "bbbbb");
    //System.out.println(replace1);

    //String trim() //在新的字符串中,去掉开头和结尾的空格字符
    String s2 = "  hello world  ";
    //System.out.println(s2 + "---" + s2.length());

    String trim = s2.trim();
    //System.out.println(trim + "---" + trim.length());


    String s3 = "helloworld";
    String s4 ="helloa";
    int result = s3.compareTo(s4);
    //System.out.println(result);
    //System.out.println('w' - 'a');

    s4 = "hello";
     result = s3.compareTo(s4);
    System.out.println(result);
    System.out.println(s3.length() - s4.length());


  }

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值