Day 01-String类常用用法

1、String类的概述

  • 多个字符表示的一串数据,也可以看成是一个字符数组

  • String类代表字符串。Java程序中的所有字符串字面值(如“qqq”)都作为此类的实例实现

  • String字符串常量特点:值在创建后不能更改,即一旦字符串确定,就会在内存区域中生成该字符串。字符串本身不能改变,但str变量中记录的地址值是可以改变的。

2、String的构造方法

package com.lei;
​
/*
    String构造方法使用:
    public String():空构造
    public String(byte[] bytes):把字节数组转成字符串
    public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
    public String(char[] value):把字符数组转成字符串
​
    public String(char[] value,int index,int count):把字符数组的一部分转成字符串,index第一位开始取的索引(包括),count为所取字符串的长度
​
    public String(String original):把字符串常量值转成字符串
​
    length():返回此字符串的长度
 */
public class day01 {
​
    public static void main(String[] args) {
        //空构造
        String s1=new String();
        s1="aaa";
        System.out.println("s1="+s1);
​
        //ASCII表:  97=a  98=b
        byte[] bytes={81,97,98,99,100,101};
        String s2=new String(bytes);
        System.out.println("s2="+s2);   //Qabcde
​
        //截取一部分
        String s3=new String(bytes,0,3);
        System.out.println("s3="+s3);   //Qab
​
        //把字符数组转成字符串
        char[] chars={'a','b','c'};
        String s4=new String (chars);
        System.out.println("s4="+s4);//abc
​
        String s5=new String (chars,0,2);
        System.out.println("s5="+s5);//ab
​
        String s6=new String("abc");
        System.out.println("s6="+s6);//abc
​
        //获取字符串的长度
        String s7="abc";
        System.out.println("字符串的长度是="+s7.length());//字符串的长度是=3
​
​
​
    }
​
}

3、String类的判断功能

package com.lei;
​
/*
    boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
    boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
    boolean contains(String str):判断大字符串中是否包含小字符串
    boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
    boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
    boolean isEmpty():判断字符串是否为空
    concat():拼接字符串
 */
public class day02 {
    public static void main(String[] args) {
​
        String s1="abc";
        String s2="Abc";
        System.out.println("equals  "+s1.equals(s2));
        System.out.println("equalsIgnoreCase  "+s1.equalsIgnoreCase(s2));
​
        //拼接字符串
        System.out.println("s1.concat(ab)"+s1.concat("ab"));
​
        //判断字符串s1中是否包含ab
        System.out.println("s1.contains(ab)"+s1.contains("ab"));
​
        //判断开头和结尾
        System.out.println("s1.startsWith(aaa)"+s1.startsWith("aaa"));
        System.out.println("s1.endsWith(bc)"+s1.endsWith("bc"));
​
        String s3="";
        //判空
        System.out.println("s3.isEmpty()"+s3.isEmpty());
    }
}

4、String类的获取功能

package com.lei;
​
/*
    char charAt(int index):获取指定索引位置的字符
    int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引
    int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引
    int indexOf(int ch,int formIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
    int indexOf(String str,int formIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引
    String substring(int start):从指定位置开始截取字符串,默认到末尾
    String substring(int start,int end):从指定位置开始到指定位置结束截取字符串
 */
public class day03 {
​
    public static void main(String[] args) {
        String s1="abcd_abcd";
        char c1=s1.charAt(1);
        System.out.println("s1.charAT(1)  "+c1);  //s1.charAT(1)  b
​
        //ASII码对应的数字
        int i1=s1.indexOf(100);
        System.out.println("s1.indexOf(100)  "+i1);  //s1.indexOf(100)  3
​
        System.out.println("s1.indexOf(abcd)  "+s1.indexOf("abcd"));  //s1.indexOf(abcd)  0
​
        System.out.println("s1.indexOf(cd)  "+s1.indexOf("cd"));  //s1.indexOf(cd)  2
​
        System.out.println("s1.indexOf(97, 4)  "+s1.indexOf(97, 4));  //s1.indexOf(97, 4)  5
​
        System.out.println("s1.indexOf(cd, 4)  "+s1.indexOf("cd", 4));  //s1.indexOf(cd, 4)  7
​
        System.out.println("s1.substring(5)   "+s1.substring(5));   //s1.substring(5)   abcd
​
        //包含开始的位置,不包含结束的位置
        System.out.println("s1.substring(1, 6)   "+s1.substring(1, 6));   //s1.substring(1, 6)   bcd_a
    }
}

5、String类的转换功能

package com.lei;
​
import java.util.Arrays;
​
/*
    byte[] getBytes():把字符串转换为字节数组
    char[] toCharArray():把字符串转换为字符数组
    static String valueOf(char[] chs):把字符数组转成字符串。
    static String valueOf(int i):把int类型的数据转成字符串。
    String toLowerCase():把字符串转成小写
    String toUpperCase():把字符串转成大写
    String concat(String str):把字符串拼接
 */
public class day04 {
    public static void main(String[] args) {
        String s1="abcd";
        byte[] bytes = s1.getBytes();
        //Arrays.toString:打印byte[]数组
        System.out.println("s1.getBytes()  "+Arrays.toString(bytes)); //s1.getBytes()  [97, 98, 99, 100]
​
        char[] chars = s1.toCharArray();
        System.out.println("s1.toCharArray()  "+Arrays.toString(chars)); //s1.toCharArray()  [a, b, c, d]
​
        char[] chars2 = {'a','b','c'};
        //生成返回值的快捷键:ctrl + alt + v
        String valueOf = String.valueOf(chars2);
        System.out.println("String.valueOf(chars2)  "+valueOf); //String.valueOf(chars2)  abc
​
        System.out.println("String.valueOf(1)   "+String.valueOf(1)); //String.valueOf(1)   1
​
        //返回转换之后的字符串,不影响原来的字符串
        //转大写
        System.out.println("s1.toUpperCase()  "+s1.toUpperCase()); //s1.toUpperCase()  ABCD
        //转小写
        System.out.println("s1.toLowerCase()  "+s1.toLowerCase()); //s1.toLowerCase()  abcd
    }
}

6、String替换功能

package com.lei;
​
/*
    String replace(char old,char new):把字符串中的某个字符用新的字符所替换
    String replace(String old,String new):把字符串中的某个字符串用新的来替换
    String trim():去除字符串两端空格
 */
public class day05 {
    public static void main(String[] args) {
​
        String s1 = "aabbcc";
​
        String result = s1.replace('c', 'w');
        System.out.println(result);  //aabbww
​
        String result2 = s1.replace("bc", "ww");
        System.out.println(result2);  //aabwwc
​
        String s2 = "   wff   ";
        System.out.println("去掉空格之后  "+s2.trim());  //去掉空格之后  wff
    }
}

7、String练习

模拟登录功能

package com.lei.Stringlei;
​
import java.util.Scanner;
​
/*
    练习:模拟登录,给三次机会,如果密码输入错误,提示还有几次
    1、应该有默认的用户名和密码
    2、提示登录,获取用户的输入
    3、equals  for
 */
public class Test01 {
    public static void main(String[] args) {
        String username = "admin";
        String password = "admin";
​
        Scanner sc = new Scanner(System.in);
​
        for (int i = 1; i <= 3; i++) {
            System.out.println("请输入用户名  ");
            String inputUserName = sc.next();
​
            System.out.println("请输入密码  ");
            String inputPassWord = sc.next();
​
            if(username.equals(inputUserName) && password.equals(inputPassWord)){
                System.out.println("登陆成功");
                break;
            }else {
                if (i == 3) {
                    System.out.println("账号锁定");
                }
                System.out.println("你还有"+(3-i)+"次机会");
            }
        }
    }
}

首字母转大写

package com.lei.Stringlei;
​
/*
    首字母转大写
 */
public class Test2 {
    public static void main(String[] args) {
        String s1 = "abcd";
​
        //获取第一个字符
        String s2 = s1.substring(0, 1);
        System.out.println(s2);  //a
​
        //获取第二个字符串之后的值
        String s3 = s1.substring(1);
        System.out.println(s3);  //bcd
​
        //拼接结果
        String result = s2.toUpperCase()+s3.toLowerCase();
        System.out.println(result);  //Abcd
    }
}

数组转字符串

package com.lei.Stringlei;
​
/*
    数组转字符串
 */
public class Test03 {
    public static void main(String[] args) {
​
        int[] arr = {1,2,3};
​
        String result = "[";
​
        for (int i = 0; i < arr.length; i++) {
            if(i == arr.length - 1){
                result += arr[i];
            }else{
                result += arr[i] + ",";
            }
        }
        result += "]";
        System.out.println(result);
​
    }
}

字符串反转

package com.lei.Stringlei;
​
/*
    把字符串反转
 */
public class Test04 {
    public static void main(String[] args) {
        String s1 = "abc";
        
        String result = "";
        for (int i = s1.length() - 1; i >= 0 ; i--) {
            System.out.println(s1.charAt(i));
            result += s1.charAt(i);
        }
        System.out.println(result);
    }
}

统计字符个数

package com.lei.Stringlei;
​
/*
    统计字符的个数,大写、小写、数字
    1、定义变量,记录每个类型的个数
    2、遍历字符串,获取每个字符,判断类型
​
 */
public class Test05 {
    public static void main(String[] args) {
        String s1 = "AAAbuu234";
        int bigCount = 0;  //大写
        int smallCount = 0;//小写
        int numCount = 0;  //数字
​
        for (int i = 0; i < s1.length(); i++) {
            char charI = s1.charAt(i);
            //ASCII
            //charI >=65 && charI <= 90
            if (charI >= 'A' && charI <= 'Z'){
                bigCount++;
            } else if (charI >= 'a' && charI <= 'z') {
                smallCount++;
            }else{
                numCount++;
            }
        }
        System.out.println("大写"+bigCount);
        System.out.println("小写"+smallCount);
        System.out.println("数字"+numCount);
​
    }
}

大串中查找出现小串出现的次数

package com.lei.Stringlei;
​
/*
    大串中查找出现小串出现的次数
 */
public class Test06 {
    public static void main(String[] args) {
        String s = "woaijavawoaijavawoaijavawoaijava";
​
        //找到第一次出现的下标
//        int index = s.indexOf("java");
//        System.out.println(index);
        int count = getCount("java",s);
        System.out.println(count);
    }
    
    public static int getCount(String target,String s){
        //记录出现的次数
        int count = 0;
        //查找第一次出现的位置
        int index = s.indexOf(target);
       //如果还存在字符串
        while (index != -1){
            count++;
            s = s.substring(index + target.length());
            index = s.indexOf(target);
        }
​
        return  count;
​
    }
}
package com.lei.Stringlei;
​
/*
    大串中查找出现小串出现的次数(改)
 */
public class Test06 {
    public static void main(String[] args) {
        String s = "woaijavawoaijavawoaijavawoaijava";
​
        //找到第一次出现的下标
//        int index = s.indexOf("java");
//        System.out.println(index);
        int count = getCount("java",s);
        System.out.println(count);
    }
​
    public static int getCount(String target,String s){
        //记录出现的次数
        int count = 0;
        //查找第一次出现的位置
        int index = 0;//s.indexOf(target);
       //如果还存在字符串
        //将index的赋值放到循环的判断条件里面
        while ((index = s.indexOf(target)) != -1){
            count++;
            s = s.substring(index + target.length());
        }
​
        return  count;
​
    }
}

字符串分割

package com.lei.Stringlei;
​
import java.util.Arrays;
​
//字符串分割
public class Test07 {
    public static void main(String[] args) {
        String s = "aka_iii_koj_ioh";
        s.split("_");
        String[] arr = s.split("_");
        System.out.println(Arrays.toString(arr));
    }
}

StringBuffer类

  • StringBuffer又称为可变字符序列,它是一个类似于String的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容。

  • StringBuffer是一个容器,容器中可以装很多字符串,并且能够对其中的字符串进行各种操作

package com.lei.Stringlei;
​
/*
    StringBuffer
    append():将字符串表示附加到序列中
    deleteCharAt():删除 char在这个序列中的指定位置
    delete():删除此序列的子字符串中的字符,子串开始于指定start并延伸到字符索引end - 1
    insert():将字符串插入到此字符序列中
    reverse():反转字符
    setCharAt():替换字符
 */
public class day06_StringBuffer01 {
    public static void main(String[] args) {
​
//        StringBuffer sb = new StringBuffer();
//        sb.append("aaa").append(12).append(true);
//        System.out.println(sb);  //aaa12true
​
        //操作的是原字符串
        StringBuffer sb = new StringBuffer("abcd");
//        StringBuffer sb2 = sb.deleteCharAt(0);
//        sb.deleteCharAt(0);
//        System.out.println(sb2); //bcd
//        System.out.println(sb);  //bcd
//        System.out.println("sb.deleteCharAt(0)   "+sb.deleteCharAt(0)); //bcd
//
//        System.out.println("sb.delete(1,2)   "+sb.delete(1,2));  //c
//        System.out.println("sb.delete(1,10)   "+sb.delete(10,10));  //StringIndexOutOfBoundsException
​
//        sb.insert(4,"AA");
//        System.out.println(sb.toString());  //abcdAA
​
//        sb.reverse();
//        System.out.println(sb.toString()); //dcba
​
        sb.setCharAt(0,'W');
        System.out.println(sb.toString());  //Wbcd
    }
}

练习:删掉指定字符

package com.lei.Stringlei;
​
public class test08_StringBuffer {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("abcdaaa");
​
        for (int i = 0; i < sb.length(); i++) {
            char c = sb.charAt(i);
            if (c == 'a'){
                sb.deleteCharAt(i);
                i--;
            }
        }
        System.out.println(sb.toString());
    }
}

选择排序

package com.lei.Stringlei;
​
import java.util.Arrays;
​
//选择算法
public class test09 {
    public static void main(String[] args) {
        int[] arr = { 1 , -10 , 7 , 9 , 8 };
​
        //记录最小值的下标
        int min = 0;
        for (int i = 0; i < arr.length; i++) {
            min = i;
            for (int j = i+1; j < arr.length; j++) {
                //判断
                if (arr[min] > arr[j]){
                    min = j;
                }
            }
            int tmp = arr[min];
            arr[min] = arr[i];
            arr[i] = tmp;
        }
        System.out.println(Arrays.toString(arr));
    }
}

二分搜索

package com.lei.Stringlei;
​
//二分搜索:先排序
public class test10 {
    public static void main(String[] args) {
        int[] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
​
        int key = 3;
        //最小下标
        int min = 0;
        //最大下标
        int max = arr.length - 1;
        //中间值下标
        int mid = 0;
​
        while (min <= max){
​
            mid = (min + max) / 2;
​
            if(key > mid){
                min = mid + 1;
            } else if (key < mid) {
                max = mid - 1;
            }else{
                System.out.println("找到了"+mid);
                return;
            }
        }
​
​
​
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王疯疯233

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

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

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

打赏作者

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

抵扣说明:

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

余额充值