Java StringAPI中最常用方法和方法使用

String常用API - String常用方法

//返回给定位置的代码单元。
char charAt(int index)
//返回从给定位置开始的码点
int codePointAt(int index)
//返回从startIndex码点开始,cpCount个码点后的码点索引。
int offsetByCodePoints(int startIndex, int cpCount)
//按照字典顺序,如果字符串在other之前,返回一个负数;如果字符串位置other之后,返回一个整数;如果两个字符串想相等,返回0;
int compareTo(String other)
//将字符串的码点作为一个流返回,调用toArray将它们放到一个数组中。
IntStream codePoints()   //JAVA8
//用数组中从offset开始的count个码点构造一个字符串
new String(int[] codePoints, int offset, int count)
//如果字符串为空或者由空格组成,返回true
boolean empty()
boolean blank() //java11
//如果字符串与other相等,返回true
boolean equals(Object other)
//如果字符串与other相等(忽略大小写),返回true。
boolean equalsIgnoreCase(String other)
//如果字符串以prefix开头或以suffix结尾,则返回true。
boolean startsWith(String prefix)
boolean endsWith(String suffix)
//返回与字符串str或码点cp匹配的第一个子串开始的位置。从索引0或fromIndex开始匹配。如果在原始字符串中不存在str,则返回-1。
int indexOf(String str)
int indexOf(String str, in fromIndex)
int indexOf(int cp)
int indexOf(int cp, in fromIndex)
//返回与字符串str或码点cp匹配的最后一个子串的开始位置。从原始字符串中末尾或fromIndx开始匹配。
int lastIndexOf(String str)
int lastIndexOf(String str, int fromIndex)
int lastindexOf(int cp)
int lastindexOf(int cp, int fromIndex)
//返回字符串代码单元的个数
int length()
//返回startIndex和endIndex-1之间码点的个数
int codePointCount(int startIndex, int endIndex)
//返回一个新字符串。这个字符串用newString代替原始字符串中所有的oldString。可以用String或StringBuilder对象作为CharSequence参数。
String replace(CharSequence oldString, CharSequence newString)
//返回一个新字符串。这个子串包含原始字符串中从beginIndex到字符串末尾或endIndex-1的所有代码单元。
String substring(int beginIndex)
String substring(int beginIndex, in endIndex)
//返回一个新字符串。这个字符串将原始字符串中的大写字母改为小写,或将原始字符串中的小写字母改为大写。
String toLowerCaase()
String toUpperCase()
//返回一个新字符串,这个字符串将删除原始字符串头部和尾部小于于等于U+0020的字符(trim)或空格(strip)
String trim()
String strip() //java11
//返回一个新字符串,用给定的定界符连接所有元素
String join(CharSequence delimiter, CharSequence... elements) //java8
//返回一个字符串,将当前字符串重复count次
String repeat(int count) //java11

String方法使用

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * ClassName StringApiTest
 * Description
 * Create by shensiback
 * Date 2021-04-20 22:38
 **/
public class StringApiTest {
    public static int num = 1;
    public static void main(String[] args) {
        String str = "this is stringApiTest ";
        print("charAt",str.charAt(0));
        print("codePointAt",str.codePointAt(0));
        print("offsetByCodePoints",str.offsetByCodePoints(1,2));
        print("compareTo",str.compareTo("this is stringApiTest1"));
        print("compareTo",str.compareTo("this is a StringApiTest"));
        print("compareTo",str.compareTo("this is stringApiTest"));
        print("codePoints",Arrays.toString(str.codePoints().toArray()));
        print("isEmpty",str.isEmpty());
        print("isEmpty","".isEmpty());
        print("equals",str.equals("123"));
        print("equals",str.equals("this is stringApiTest"));
        print("equalsIgnoreCase",str.equalsIgnoreCase("THIS IS STRINGAPITEST"));
        print("equalsIgnoreCase",str.equalsIgnoreCase("THIS is stringApiTest"));
        print("startsWith",str.startsWith("this is"));
        print("startsWith",str.startsWith("this not is"));
        print("endsWith",str.endsWith("Test"));
        print("endsWith",str.endsWith("test"));
        print("indexOf",str.indexOf("is"));
        print("indexOf",str.indexOf("is",10));
        print("indexOf",str.indexOf(32));
        print("indexOf",str.indexOf(32,10));
        print("lastIndexOf",str.lastIndexOf("est"));
        print("lastIndexOf",str.lastIndexOf("est",10));
        print("lastIndexOf",str.lastIndexOf(32));
        print("lastIndexOf",str.lastIndexOf(32,10));
        print("length",str.length());
        print("codePointCount",str.codePointCount(0,3));
        print("replace",str.replace(" is "," not is "));
        print("substring",str.substring(5));
        print("substring",str.substring(5,7));
        print("toLowerCase",str.toLowerCase());
        print("toUpperCase",str.toUpperCase());
        print("trim",str.trim());
        print("join",String.join(str,"header "," -- 001 "," -- 002 "));
    }


    public static <T> void print(String name,T t){
        System.out.print(name + " : ");
        System.out.println(t.toString());
    }

}

输出

charAt : t
codePointAt : 116
offsetByCodePoints : 3
compareTo : -17
compareTo : 18
compareTo : 1
codePoints : [116, 104, 105, 115, 32, 105, 115, 32, 115, 116, 114, 105, 110, 103, 65, 112, 105, 84, 101, 115, 116, 32]
isEmpty : false
isEmpty : true
equals : false
equals : false
equalsIgnoreCase : false
equalsIgnoreCase : false
startsWith : true
startsWith : false
endsWith : false
endsWith : false
indexOf : 2
indexOf : -1
indexOf : 4
indexOf : 21
lastIndexOf : 18
lastIndexOf : -1
lastIndexOf : 21
lastIndexOf : 7
length : 22
codePointCount : 3
replace : this not is stringApiTest 
substring : is stringApiTest 
substring : is
toLowerCase : this is stringapitest 
toUpperCase : THIS IS STRINGAPITEST 
trim : this is stringApiTest
join : header this is stringApiTest  -- 001 this is stringApiTest  -- 002 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值