String Class - 字符串类型 - 下半部分


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

字符,字节与字符串 之间的相互转换

字符与字符串之间的相互转换

将字符数组中的所有内容变为字符串

通过调用构造方法: public String(char value[]);
语法:String str = new String(字符数组名)
类型:构造

代码如下

public class Test {
    public static void main(String[] args) {
        char[] val = {'a','b','c'};
        String str = new String(val);
        System.out.println(str);
    }
}

效果图如下

将部分字符数组中的内容变为字符串
通过调用构造方法: public String(char value[],int offset,int count);
语法:String str = new String(字符数组名,偏移量/下标,偏移量的位置开始,转化多少个字符为字符串)
类型:构造
 

代码如下

public class Test {
    public static void main(String[] args) {
        char[] val = {'a','b','c','d','e'};
        String str = new String(val,1,4);
        System.out.println(str);
    }
}

效果图如下

取得字符串指定索引位置的字符


语法:char 字符变量 = 字符串变量.charAt(下标)
获取字符串对应的下标元素。

代码如下
public class Test {
    public static void main(String[] args) {
        String str = "hello";
        char ch = str.charAt(4);// 获取下标为4的字符 o
        System.out.println(ch);
    }
}

效果图:

将字符串变为字符数组


语法: Char[] 数组名 = 字符串变量.toCharArray();

代码如下
import java.util.Arrays;
public class Test {
    public static void main(String[] args) {
        String str = "hello";
        // 将 str指向的字符串对象,转换成 字符数组对象,并将其地址返回。
        char[] chars = str.toCharArray();
        System.out.println(Arrays.toString(chars));
    }
}

效果图:

练习(给定一个字符串,判断其是否全部由数字所组成)
方式1
public class Test {
    public  static boolean isNumberChar(String s){
        for (int i = 0; i < s.length(); i++) {
             char c =s.charAt(i);
             if ('0'>c || c>'9'){
                 return false;
             }
        }
        return true;
    }

    public static void main(String[] args) {
        String str = "12345";
        System.out.println(isNumberChar(str));
    }
}

效果图

方式2(使用字符封装,所包含的功能 )

代码如下:

public class Test {
    public  static boolean isNumberChar(String s){
        for (int i = 0; i < s.length(); i++) {
             char c =s.charAt(i);
             boolean flg =  Character.isDigit(c);
             if(flg == false){
                 return flg;
             }
        }
        return true;
    }

    public static void main(String[] args) {
        String str = "1234a";
        System.out.println(isNumberChar(str));
    }
}

Character 的 is用法

字节 与 字符串之间的转换

将字节数组变为字符串

通过调用构造方法 public String(byte bytes[]);
语法 : String 字符串变量名 = new String(字节类型的数组名)
类型:构造

public class Test {
    public static void main(String[] args) {
        byte[] bytes = {97,98,99,100};
        String str = new String(bytes);
        System.out.println(str);
    }
}

将 部分字节数组中的内容变为字符串
通过调用构造方法 public String(byte bytes[], int offset, int length);
方法: String 字符串变量 = new String(字节数组名, 偏移量/下标, 从指定的下标位置开始,转换多少个元素,为字符串)
类型:构造
 

public class Test {
    public static void main(String[] args) {
        byte[] bytes = {97,98,99,100};
        String str = new String(bytes,1,3);
        System.out.println(str);
    }
}

将字符串 -转换成 字节数组

语法: byte[] bytes = 字符串变量.getBytes();

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        String str = "abcd";
        byte[] bytes = str.getBytes();
        System.out.println(Arrays.toString(bytes));
    }
 }
效果图

编码转换处理
public class Test {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "abcd";
        byte[] bytes = str.getBytes("utf-8");// 如果出现警告,那是你们没有抛异常,
        //选择报错部分,alt + enter,第一个add的那个就是 处理抛出异常,你就会发现 main 方法后面多出一个 多出一部分
        System.out.println(Arrays.toString(bytes));
    }
}

效果图

附加图

小结

byte[] 是把 String 按照一个字节一个字节的方式处理, 这种适合在网络传输, 数据存储这样的场景下使用. 更适合针对二进制数据来操作.
char[] 是吧 String 按照一个字符一个字符的方式处理, 更适合针对文本数据来操作, 尤其是包含中文的时候

字符串常见的操作

字符串比较


总得来说分为两种 1. 真假比较,2.大小比较

equals,区分字符大小写的比较。(两个字符串的字符必须是大小一致的情况下,才能算相等)

语法: 字符串变量A(非null).equals(字符串常量B)
调用者/字符串变量A,不能为 null,是为了避免空指针异常.

下面我们再来了解 equals 的 其它用法:不区分大小写比较 - 真假比较

语法:字符串变量A(非null).equalsIgnoreCase(字符串变量B),
调用者/字符串变量A,不能为 null,是为了避免空指针异常.
IgnoreCase 就是忽略大小写的意思

compareTo:比较两个字符串大小关系 - 大小比较
语法:字符串变量A(非null).compareTo(字符串变量B)
调用者/字符串变量A,不能为 null,是为了避免空指针异常.
结果大于0,说明字符串A 比 字符串B 要大
结果小于0,说明字符串A 比 字符串B 要小
结果等于0,说明字符串A 比 字符串B 相等

public class Test {
    public static void main(String[] args) {
        String str = "abababcabababcd";
        String tmp = "abc";// 字符串tmp  的 abc,字符串str 是有的
        boolean b = str.contains(tmp);// contains 会帮我们找到在 str中,从前往后找第一次出现的 abc
        // 找到了就返回 true,没找到就false。
        System.out.println(b);
    }
}

字符串查找

使用 contains 方法 判断子字符串是否存在

语法: 字符串变量A(非null).contains(字符串变量B)
调用者/字符串变量A,不能为 null,是为了避免空指针异常.

public class Test {
    public static void main(String[] args) {
        String str = "abababcabababcd";
        String tmp = "abc";// 字符串tmp  的 abc,字符串str 是有的
        boolean b = str.contains(tmp);// contains 会帮我们找到在 str中,从前往后找第一次出现的 abc
        // 找到了就返回 true,没找到就false。
        System.out.println(b);
    }
}

使用 indexOf 方法 从头开始查找指定字符串的位置(从前往后找第一次出现的 abc的位置),查到了就返回位置的开始索引。查不到就返回-1.
语法:字符串变量A(非null).indexOf(字符串变量B)
调用者/字符串变量A,不能为 null,是为了避免空指针异常.
 

使用 indexOf 方法 从指定的位置,查找字符串位置
语法:字符串变量A(非null).indexOf‘(字符串变量B,指定的位置)’
(调用者/字符串变量A,不能为 null,是为了避免空指针异常.)
其实就是上一个方法中,多加了一个指定搜索位置。
要求从指定的位置开始查找子串。
找到就返回开始位置的索引,没找就返回 -1.


 

public class Test {
    public static void main(String[] args) {
        String str = "abababcabababcd";
        String tmp = "abc";
        int index = str.indexOf(tmp,5);
        System.out.println(index);
    }
}

 

使用 lastIndexOf 方法 从后向前查找子字符串的位置

语法: 字符串变量A(非null).lastIndex‘Of(字符串变量B): -—— last 就是最后的意思;。
调用者/字符串变量A,不能为 null,是为了避免空指针异常.

public class Test {
    public static void main(String[] args) {
        String str = "abababcabababcd";
        String tmp = "abc" ;
        int lastIndex = str.lastIndexOf(tmp);
        System.out.println(lastIndex);
    }
}

既然 indexOf 方法 可以指定位置搜索,那么 lastIndexOf 方法 也可以。

使用 startWith 方法判断是否以指定字符串开头

语法:字符串变量A(非null).startWith(“字符串”);

public class Test {
    public static void main(String[] args) {
        String str = "abababcabababcd";
        System.out.println(str.startsWith("a"));
        System.out.println(str.startsWith("abab"));
        System.out.println(str.startsWith("abc"));
    }
}

使用 startsWith 方法 从指定位置开始判断是否以指定字符串开头

使用 endsWith 方法 来判断是否以指定字符串结尾。

语法: 字符串变量A(非null).endsWith(“字符串”);


字符串替换

使用一个指定的新字符串替换掉已有的字符串数据

使用 replace 方法 替换所有的指定内容(字符)

语法:String 字符串变量A = replace(‘旧字符’,‘新字符’)

使用 replace 方法 替换所有的指定内容(字符串)

语法:String 字符串变量A = replace(’“旧字符串”,“新字符串”)

使用 replaceAll 方法 替换所有的指定内容(字符串)

语法:String 字符串变量A = replaceAll(“旧字符串”,“新字符串”)


字符串拆分

将一个完整的字符串按照规定的分隔符分为若干个子字符串。

使用 split 方法 将字符串全部拆分

语法 字符串变量A.split(“分隔字符”)

但是我啊,还想进一步拆解,将等号两端的数据分割开来

使用 split 方法 将字符串部分拆分,分割后,形成的数组长度就是limit极限

语法:字符串变量A.split("分割字符’,int limit );

注意事项总结:

1. 分割字符"|","*","+“都得加上转义字符,前面加上” \ “.
2. 而如果是” \ “,那么就得写成” \ \ \ \ “.
3. 如果一个字符串中有多个分隔符,可以用”|"作为连字符


字符串截取

从一个完整的字符串之中截取出部分内容。

使用 substring 方法 从指定索引截取到结尾

语法: 字符串变量A.substring(指定索引/下标);

使用 substring 方法 截取部分内容

字符串的其他操作方法

使用 trim 方法 去掉字符串中的左右空格,保留中间空格。

语法 String 字符串变量B = 字符串变量A.trim();

public class Test {
    public static void main(String[] args) {
        String str = "   a    d      ";
        String str1 = str.trim();
        System.out.print(str1);
        System.out.;print("=====");// 用来验证后面的空格也被删除了
    }
}

使用 toUpperCase 方法 实现 字符串转大写

public class Test {
    public static void main(String[] args) {
        String str = "abcDEF123道";
        String str1 = str.toUpperCase();
        System.out.println(str1);
    }
 }

使用 toLowerCase 方法 实现字符串转小写

public class Test {
    public static void main(String[] args) {
        String str = "abcDEF123道";
        String str1 = str.toLowerCase();
        System.out.println(str1);
    }
}

 

使用 concat 方法 字符串连接,等同于"+",不入池(拼接出的新字符串,不放入字符串常量池,要想入池,需要借助 intern 方法,手动入池。)

public class Test {
    public static void main(String[] args) {
        String str1 = "ab";
        String str2 = "cd";
        String str3 = str1.concat(str2);
        System.out.println(str3);
    }
}

使用 length() 方法,来获取字符串长度

虽然大家都知道,但是有坑。
来看看下面代码中 两行代码的区别

public class Test {
    public static void main(String[] args) {
        String str1 = "ab";
        String[] str2 = new String[5];
        System.out.println(str1.length());// 求字符串长度,是用的length方法
        System.out.println(str2.length);// 求数组长度,length 是数组的一个属性,是一个整形变量。
        注意面试中的笔试环节,手撕代码的时候,注意如果是求字符串长度,需要在length后面加括号。
        数组则不用。
    }
}

使用 isEmpty方法 来判断是否为空字符串

这里的空字符串不是null,而是字符串长度为0,例如"":双引号中什么都没有。
而这种 " " 这也不是空字符串,你这字符串是包含字符串空格的,

如果你搞错了,以为空指针时null,则会发生空指针异常


总结

本文至此,还没结束。等我再写一篇。敬请期待!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值