美文网首页
API(一)— String

API(一)— String

作者: Anwfly | 来源:发表于2020-07-28 17:28 被阅读0次

一、概述

1. 概述

java.lang.String 类代表字符串。Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实 例。
​ 类 String 中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串以及创建具有翻 译为大写或小写的所有字符的字符串的副本。

2. String类的特点

①字符串不变:字符串的值在创建后不能被更改。

String str1 = "abc";
str1 += "d";
System.out.println(str1); // "abcd"
// 内存中有"abc","abcd"两个对象,s1从指向"abc",改变指向,指向了"abcd"。

②因为String对象是不可变的,所以它们可以被共享。

String s1 = "abc";
String s2 = "abc";
// 内存中只有一个"abc"对象被创建,同时被s1和s2共享。
System.out.println("s1 == s2:"+(s1== s2));

​⑤"abc" 等效于 char[] data={ 'a' , 'b' , 'c' } 。

例如:
String str = "abc";
相当于:
char data[] = {'a', 'b', 'c'}; 
String str = new String(data); // String底层是靠字符数组实现的。

二、String类构造

1. String常用构造方法★★

//通过byte数组构造字符串对象。
public String(byte[ ] bytes)
//通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
public String(byte[] bytes, int offset, int length) 
//通过char数组构造字符串对象。
public String(char[ ] value)
//构造一个original的副本。即:拷贝一个original。
public String(Sting original)
//通过StringBuffer数组构造字符串对象。
public String(StringBuffer buffer):

例如:

byte[] arr1 = {'a','b','c','d','e','f','g','h','i','j'};
char[] arr2 = {'0','1','2','3','4','5','6','7','8','9'};

String sb = new String(arr1);            //abcdefghij
String sb_sub = new String(arr1,3,2);    //de
String sc = new String(arr2);            //0123456789
String sc_sub = new String(arr2,3,2);    //34
String sb_copy = new String(sb);      //abcdefghij  

System.out.println("sb:"+sb);
System.out.println("sb_sub:"+sb_sub);
System.out.println("sc:"+sc);
System.out.println("sc_sub:"+sc_sub);
System.out.println("sb_copy:"+sb_copy);

注意:String字面值对象和构造方法创建对象的区别(掌握)
String s1 = “abc”; 这个字符串对象是一个常量,放在字符串常量池中,在编译时就可以确认,所以放入常量去区,当在定义String s2=“abc”;时,s1与s2指向的是同一个字符串常量,他们在内存中的地址相同,内容也相同。String s3 = new String(“abc”);这个字符串虽然内容也是”abc”但是在编译时并没有放入常量池,而是在运行时才创建的一个对象,所以s1与s3不是同一个字符串对象,他们指向的内存地址也不会相同。

三、String类常用功能

1. String类的常用功能-判断★★★

//判断内容是否相等,区分大小写,anObject:比较的内容
public boolean equals(Object anObject)
//判断内容是否相等,不区分大小写,anotherString:另一个字符串
public boolean equalsIgnoreCase(String anotherString)
//是否包含子字符串,s:子字符串
public boolean contains(CharSequence s)
//判断字符串长度是否为0
public boolean isEmpty()
//判断是否以该字符串为前缀,prefix:前缀字符串
public boolean startsWith(String prefix)
//判断是否以该字符串为后缀,suffix:后缀字符串
public boolean endsWith(String suffix)
//判断字符串与StringBuffer内容是否相同,sb:StringBuffer对象
public boolean contentEquals(StringBuffer sb) 

演示代码如下:

public static void main(String[] args) {
    // 创建字符串对象
    String s1 = "helloworld";
    String s2 = "helloworld";
    String s3 = "HelloWorld";

    // boolean equals(Object obj):比较字符串内容是否相同,区分大小写
    System.out.println("equals:" + s1.equals(s2));//true
    System.out.println("equals:" + s1.equals(s3));//false
    System.out.println("-----------------------");

    // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
    System.out.println("equals:" + s1.equalsIgnoreCase(s2));//true
    System.out.println("equals:" + s1.equalsIgnoreCase(s3));//true
    System.out.println("-----------------------");

    // boolean contains(String str):判断大字符串中是否包含小字符串
    System.out.println("contains:" + s1.contains("hello"));//true
    System.out.println("contains:" + s1.contains("hw"));//fasle
    System.out.println("-----------------------");

    // boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
    System.out.println("startsWith:" + s1.startsWith("h"));//true
    System.out.println("startsWith:" + s1.startsWith("hello"));//true
    System.out.println("startsWith:" + s1.startsWith("world"));//false
    System.out.println("-----------------------");
    // 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩

    // boolean isEmpty():判断字符串是否为空。
    System.out.println("isEmpty:" + s1.isEmpty());//false

    String s4 = "";
    String s5 = null;
    System.out.println("isEmpty:" + s4.isEmpty());//true
    // NullPointerException
    // s5对象都不存在,所以不能调用方法,空指针异常
    System.out.println("isEmpty:" + s5.isEmpty());
}

2. String类的常用功能-获取★★★

//获取字符串长度
public int length()
//获取指定索引对应的字符
public native char charAt(int index);
//判断int值对应的字符在该字符串中出现的位置
public int indexOf(int ch)
//判断子字符串在该字符串中出现的位置
public int indexOf(String str)
//截取字符串,beginIndex:开始位置的索引,endIndex:结束位置得索引
public String substring(int beginIndex, int endIndex)
//将字符数组转换成字符串,可以从offset开始,取count个结束,offset:开始的索引,count:个数 
public static String copyValueOf(char data[], int offset, int count)

演示代码如下:

public static void main(String[] args) {
    // 定义一个字符串对象
    String s = "helloworld";

    // int length():获取字符串的长度。
    System.out.println("s.length:" + s.length());
    System.out.println("----------------------");

    // char charAt(int index):获取指定索引位置的字符
    System.out.println("charAt:" + s.charAt(7));
    System.out.println("----------------------");

    // int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
    System.out.println("indexOf:" + s.indexOf('l'));
    System.out.println("----------------------");

    // int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
    System.out.println("indexOf:" + s.indexOf("owo"));
    System.out.println("----------------------");

    // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
    System.out.println("indexOf:" + s.indexOf('l', 4));
    System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
    System.out.println("indexOf:" + s.indexOf('l', 40)); // -1
    System.out.println("----------------------");

    // 自己练习:int indexOf(String str,int
    // fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。

    // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
    System.out.println("substring:" + s.substring(5));
    System.out.println("substring:" + s.substring(0));
    System.out.println("----------------------");

    // String substring(int start,int
    // end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
    System.out.println("substring:" + s.substring(3, 8));
    System.out.println("substring:" + s.substring(0, s.length()));
}      

3. String类的常用功能-转换★★★

//转换为byte[]数组
public byte[] getBytes()
//转换成字符数组
public native char[] toCharArray()
//将基本数据类型转换成字符串,byte数组或char数组也可以,data[]:字符串数组,i:想要转成字符串的数字
public static String valueOf(char data[])
public static String valueOf(int i)
//转换成小写
public String toLowerCase()
//转换成大写
public String toUpperCase()
//拼接 ,str:被拼接的字符串
public native String concat(String str)  

示例代码如下:

public static void main(String[] args) {
    // 定义一个字符串对象
    String s = "JavaSE";
    // byte[] getBytes():把字符串转换为字节数组。
    byte[] bys = s.getBytes();
    for (int x = 0; x < bys.length; x++) {
      System.out.println(bys[x]);
    }
    System.out.println("----------------");

    // char[] toCharArray():把字符串转换为字符数组。
    char[] chs = s.toCharArray();
    for (int x = 0; x < chs.length; x++) {
      System.out.println(chs[x]);
    }
    System.out.println("----------------");

    // static String valueOf(char[] chs):把字符数组转成字符串。
    String ss = String.valueOf(chs);
    System.out.println(ss);
    System.out.println("----------------");

    // static String valueOf(int i):把int类型的数据转成字符串。
    int i = 100;
    String sss = String.valueOf(i);
    System.out.println(sss);
    System.out.println("----------------");

    // String toLowerCase():把字符串转成小写。
    System.out.println("toLowerCase:" + s.toLowerCase());
    System.out.println("s:" + s);
    // System.out.println("----------------");
    // String toUpperCase():把字符串转成大写。
    System.out.println("toUpperCase:" + s.toUpperCase());
    System.out.println("----------------");

    // String concat(String str):把字符串拼接。
    String s1 = "hello";
    String s2 = "world";
    String s3 = s1 + s2;
    String s4 = s1.concat(s2);
    System.out.println("s3:"+s3);
    System.out.println("s4:"+s4);
}     

4. String类的常用功能-其他★★★

//替换,oldChar:要被替换的子字符串,newChar:替换后的子字符串
public String replace(char oldChar, char newChar)
//去除首尾空格
public String trim()
//对比两个字符串大小排序,返回值:大于0 前者大,后者小,等于0两者相等,小于0 前者小,后者大
//anotherString:要比较的字符串
public native int compareTo(String anotherString)

示例代码如下:

public static void main(String[] args) {
    // 替换功能
    String s1 = "helloworld";
    String s2 = s1.replace('l', 'k');
    String s3 = s1.replace("owo", "ak47");
    System.out.println("s1:" + s1);
    System.out.println("s2:" + s2);
    System.out.println("s3:" + s3);
    System.out.println("---------------");

    // 去除字符串两空格
    String s4 = " hello world  ";
    String s5 = s4.trim();
    System.out.println("s4:" + s4 + "---");
    System.out.println("s5:" + s5 + "---");

    // 按字典顺序比较两个字符串
    String s6 = "hello";
    String s7 = "hello";
    String s8 = "abc";
    String s9 = "xyz";
    System.out.println(s6.compareTo(s7));// 0
    System.out.println(s6.compareTo(s8));// 7
    System.out.println(s6.compareTo(s9));// -16
}       

相关文章

网友评论

      本文标题:API(一)— String

      本文链接:https://www.haomeiwen.com/subject/afsyfctx.html