String构造方法、Java语言字符串的特征、String类的判断功能、获取功能、转换功能、替换功能、比较功能、去除空字符串

目录

String类

String构造方法

Java语言字符串的特征

String类的判断功能

String类的获取功能

String类的转换功能

String类的替换功能

String类去除空字符串

String类的比较功能


String类

​    字符串是由多个字符组成的一串数据(字符序列)     字符串可以看成是字符数组

String构造方法

public String()
public String(byte[] bytes)
public String(byte[] bytes, int offset, int length)
public String(char[] value)
public String(char[] value, int offset, int count)

public String(String original)了解 没有意义

package com.wolf.string;

public class Demo1 {
    public static void main(String[] args) {
        //public String()
        String str = new String();
        String s = "";
        //String 类中,覆盖了object类的equals方法,string中的equals比较的是字符串内容
        System.out.println("".equals(str));//true

        //public String(byte[] bytes)
        byte[] byteStr = {97, 98, 99, 100, 101};//a,b,c,d,e
        s = new String(byteStr);
        System.out.println(s);//abcde

        //public String(byte[] bytes, int offset, int length)
        //利用字节数数组的一部分,创建字符序列,从byte数组的offset开始的length个字节值
        s = new String(byteStr, 1, 3);
        System.out.println(s);//bcd

        //public String(char[] value)
        char[] chars = {'a', 'b', 'c', 'd', 'e'};
        s = new String(chars);
        System.out.println(s);//abcde

        //public String(char[] value, int offset, int count)
        s = new String(chars, 1, 3);
        System.out.println(s);//bcd
    }
}

Java语言字符串的特征

字符串是常量,它的值在创建之后不能更改

String s = “hello”; s += “world”; 问s 的结果是多少? 面试题
String s = new string(“hello”)和 String s = “hello”;的区别?
字符串比较之看程序写结果
字符串拼接之看程序写结果

package com.wolf.string.basic;

import java.util.Scanner;

public class Demo2Character {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String tmp = s;
        s += ",world";
        System.out.println(s);
        //lisi
        //lisi,world
        System.out.println(tmp == s);//false
    }
}

String s = "hello";
s += ",world";
System.out.println(s);//hello,world

public static void exercise() {
    String s1 = new String("hello");
    String s2 = new String("hello");
    System.out.println(s1 == s2);//false 堆(new了两个)
    System.out.println(s1.equals(s2));//true
    String s3 = new String("hello");
    String s4 = "hello";
    System.out.println(s3 == s4);//false 堆、常量池
    System.out.println(s3.equals(s4));//true
    String s5 = "hello";
    String s6 = "hello";
    System.out.println(s5 == s6);//true
    System.out.println(s5.equals(s6));//true
    
    String s1 = "hello";
	String s2 = "hello";
	String s3 = "helloworld";
	System.out.println(s3 == (s1 + s2));//false  堆中s1 + s2结果的地址和常量池s3比较
	System.out.println(s3.equals(s1 + s2));//true
	System.out.println(s3 == ("hello" + "world"));//true
	System.out.println(s3.equals("hello" + "world"));//true
   /* 
   字符串拼接何时会在堆上,创建新的字符对象,何时不会呢?
	1.当参与字符串拼接的两个字符串中,至少有一个是以字符串的引用变量的形式出现,此时拼接运算,必然会在堆上创建新的字符串对象       s1 + s2   s1 + "hello"
	2.只有参与字符串拼接运算两个字符串,都是字符串常量的时候,此时不会在堆上创建新的字符串对象,而是直接拼接   
	*/
}

String类的判断功能

boolean equals (Object obj)  //比较字符串内容
boolean equalsIgnorecase(String str)  //比较字符串内容,但是忽略字符串大小写
boolean contains(String str)  //判断当前字符串中是否包含,参数字符串
boolean startswith(String str) //判断当前字符串中是否以参数开头
boolean endswith(String str)  //判断当前字符串中是否以参数结尾
boolean isEmpty()

package com.wolf.string.api;

public class Demo1Judge {
    public static void main(String[] args) {
        String s = "zhangsan";
        String s2 = new String(s);
        System.out.println(s.equals(s2));//true

        //boolean equals(Object obj)不忽略大小写
        System.out.println("ZhangSan".equals(s));//false

        //boolean equalsIgnorecase(String str)忽略大小写
        System.out.println("ZhangSan".equalsIgnoreCase(s));//true

        //boolean contains(String str)
        System.out.println("zhangsan".contains("zh"));//true
        System.out.println("zhangsan".contains("zhs"));//false

        //boolean startswith(String str)
        System.out.println(s.startsWith("zh"));//true
        System.out.println(s.startsWith("zhs"));//false

        //boolean endswith(String str)
        System.out.println(s.endsWith("san"));//true
        System.out.println(s.endsWith("lan"));//false

        //boolean isEmpty ()
        System.out.println(s.isEmpty());//false
        System.out.println("".isEmpty());//true
        System.out.println(new String().isEmpty());//true
    }
}

String类的获取功能

int length()   //返回当前字符串中包含的字符个数
char charAt(int index)   //获取字符串指定位置的字符(字符串中的字符,从左到右,从0开始编号)
int indexOf(int ch)  //在当前字符串中,查找参数字符,如果当前字符串中存在,则返回首次出现的位置,若不存在则返回-1
int indexOf(String str)  //在当前字符串中,查找参数字符串首次出现的位置,如果找到返回参数字符串首次出现的首字母的位置,否则返回-1
int indexOf(int ch, int fromIndex)   //指定从字符串的fromIndex位置开始,向后查找指定字符,找到返回其从formIndex开始,首次出现的位置,否则返回-1
int indexOf(String str, int fromIndex)  //指定从字符串的fromIndex位置开始,向后查找指定字符串,找到返回其从formIndex开始,首次出现的位置,否则返回-1
String substring(int beginIndex)  //生成当前字符串的子串,字串的取值是原字符串的[start,length()-1]
String substring(int beginIndex, int endIndex)  //生成当前字符串的子作,字巾的取值是原字符串的[start,end)

package com.wolf.string.api;

public class Demo2Get {
    public static void main(String[] args) {
        String s = "zhangsan";

        //int length()
        System.out.println(s.length());//8

        //char charAt(int index)
        System.out.println(s.charAt(3));//n

        //int indexOf(int ch)
        System.out.println(s.indexOf('a'));//2

        //int indexOf(String str)
        System.out.println(s.indexOf("an"));//2

        //int indexOf(int ch, int fromIndex)
        System.out.println(s.indexOf('a', 3));//6

        //int indexOf(String str, int fromIndex)
        System.out.println(s.indexOf("an", 3));//6

        //String substring(int beginIndex)
        System.out.println(s.substring(1));//hangsan

        //String substring(int beginIndex, int endIndex)
        System.out.println(s.substring(1, 5));//hang
    }
}

String类的转换功能

byte[] getBytes()  //获取表示一个字符串中,多个字符对应多个字节值
char[] toCharArray()   //获取表示一个字符串中,多个字符对应多个字符
static String valueOf(char[] chs)   //把一个字符数组转化成一个字符串
static String valueOf(int i)   //把一个整数值,转化成其字符串表示形式
String toLowerCase()   //把一个字符串的所有字符转化成小写,返回该新的字符串对象
String toUpperCase()   //把一个字符串的所有字符转化成大写,返回该新的字符串对象
String concat(String str)  //*了解*   完成字符串拼接

package com.wolf.string.api;

import java.util.Arrays;

public class Demo3 {
    public static void main(String[] args) {
        String s = "cikewuliuqi";

        //byte[] getBytes()
        byte[] bytes = s.getBytes();
        System.out.println(Arrays.toString(bytes));
        //[99, 105, 107, 101, 119, 117, 108, 105, 117, 113, 105]

        //char[] toCharArray()
        char[] chars = s.toCharArray();
        System.out.println(Arrays.toString(chars));//[c, i, k, e, w, u, l, i, u, q, i]

        //static String valueOf(char[] chs)
        String str = String.valueOf(chars);
        System.out.println(str);//cikewuliuqi

        //static String valueOf(int i)
        int i = 10;
        str = String.valueOf(i);
        System.out.println(str);//10


        //String toLowerCase()
        System.out.println(s.toUpperCase());//CIKEWULIUQI

        //String toUpperCase()
        String s1 = "sfcDSD";
        System.out.println(s1.toLowerCase());//sfcdsd

        //String concat(String str)
        String concat = s.concat(s1);
        System.out.println(concat);//cikewuliuqisfcDSD
    }
}

String类的替换功能

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

package com.wolf.string.api;

public class Demo4other {
    public static void main(String[] args) {
        String s = "meihuashisan";

        //String replace(char old, char new)
        String replace = s.replace('i', 'o');//meohuashosan
        System.out.println(replace);

        //String replace (String old,String new)
        replace = s.replace("hua", "zhangsan");
        System.out.println(replace);//meizhangsanshisan
        
        //String trim()
        String str = "  hello,world   ";
        System.out.println(str);//  hello,world   
        System.out.println(str.trim());//hello,world
    }
}

String类去除空字符串

String trim()

String类的比较功能

int compareTo(String str)
int compareToIgnoreCase (String str)
  //忽略大小写

1.字符事的大小如何比较?
    按照字典序,比较字符串的大小。字典序原本的含义实质,英文单词在字典中出现的先后顺序(在字典中,先出现的字符串小,后出现的字符串大)
    具体到编程语言,是根据两个字符串字符串从左往右数,第一个对应位置的不同字符,来决定两字符串的大小

compareTo几乎就是按照字典序,来比较两个字符串大小的

            字符串对象.compareTo(字符串对象)

对于CompareTo方法:
String类实现了一个

interface Comparable <String> {
    int compareTo (String anotherStr)
}

该接口,定义了一种比较规则,该规则规定:
        1.对于大于这种比较结果 > 0 表示大于关系
        2.对于小于这种比较结果 < 0 表示小于关系
        3.对于等于这种比较结果  0  表示相等关系

//int compareTo(String str)
String currebtStr="helloworld";
str="helloab";
int result = currebtStr.compareTo(str);
System.out.println(result);//22
System.out.println('w'-'a');//22

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值