做網(wǎng)站做的好的公司有哪些免費(fèi)網(wǎng)站排名優(yōu)化軟件
字符串轉(zhuǎn)義符號為 \
常見的轉(zhuǎn)義字符
轉(zhuǎn)移字符對應(yīng)的英文是 escape character , 轉(zhuǎn)義字符串( Escape Sequence )
字母前面加上捺斜線 "" 來表示常見的那些不能顯示的 ASCII 字符 . 稱為轉(zhuǎn)義字符 . 如 \0,\t,\n 等,就稱為轉(zhuǎn)
義字符,因?yàn)楹竺娴淖址?#xff0c;都不是它本來的 ASCII 字符意思了。
常用方法
1. length() 字符個(gè)數(shù)
2. equals equalsIgnoreCase 方法
3. trim 方法
4. substring 方法
5. concat ()方法用于將指定的字符串參數(shù)連接到字符串上。
6. contains() 判斷是否包括某字符串,返回的是 true false
7. indexOf ()返回子串的位置索引,沒有返回 -1
8. lastIndexOf () 返回子串的位置索引,沒有返回 -1
9. replace ()替換返回
10. split ()拆分為 String[]
11. toLowerCase () toUpperCase ()轉(zhuǎn)換小寫大寫
轉(zhuǎn)義字符
所有的轉(zhuǎn)義字符和所對應(yīng)的意義:

文本塊 text block
文字塊( text blocks )這個(gè)特性,首先在 JDK 13 中以預(yù)覽版的形式發(fā)布。在 JDK 14 中,改進(jìn)的文
字塊再次以預(yù)覽版的形式發(fā)布。最后,文字塊在 JDK 15 正式發(fā)布。
在沒有使用文字塊之前,我們項(xiàng)目中也許有這樣的一串代碼:
String htmlString =
"<!DOCTYPE html>\n" +
"<html>\n" +
" <body>\n" +
" <h1>\"Hello World!\"</h1>\n" +
" </body>\n" +
"</html>\n";
System.out.println(htmlString);
字符串截取案例// linux /usr/local/jdk/
//windows d:\\jdk\\jdk-17
String file = "c:/user/abc/upload/xxsfasf.afasf-asfas.jpg";
//求出文件目錄 c:/user/abc/upload/
//求出文件名稱 xxsfasf.afasf-asfas.jpg
//求出文件的擴(kuò)展名 jpg
//將字符串替換為c:/user/abc/upload/20211202154333.jpgjava11 字符串新特性方法package cn.ex;
import java.io.File;
import java.util.UUID;
/**
* @author webrx
* @since 17
*/
public class Ex2 {
public static void main(String[] args) {
String p = "d:/xxx/user/user_abc.jpg";
System.out.println(p);
//File.separator 是根據(jù)系統(tǒng)返回相關(guān)的符號 linux / windows \ \\
String fc = File.separator;
fc = p.indexOf(fc) == -1 ? "/" : fc;
System.out.println(fc);
String path = p.substring(0, p.lastIndexOf(fc)).concat(fc);
System.out.println(path);
String name = p.substring(p.lastIndexOf(fc) + 1);
System.out.println(name);
String ext = name.lastIndexOf(".") == -1 ? "" :
name.substring(name.lastIndexOf(".") + 1);
System.out.println(ext);
UUID uuid = UUID.randomUUID();
String newp = String.format("%s%s.%s", path, uuid, ext);
System.out.println(newp);
}
}
java 11 string api// 判斷字符串是否為空白
" ".isBlank(); // true
// 去除首尾空格 .trim()
" Javastack ".strip(); // "Javastack"
// 去除尾部空格
" Javastack ".stripTrailing(); // " Javastack"
// 去除首部空格
" Javastack ".stripLeading(); // "Javastack "
// 復(fù)制字符串
"Java".repeat(3); // "JavaJavaJava"
// 行數(shù)統(tǒng)計(jì)
"A\nB\nC".lines().count(); // 3
String str = "Java";
// 小于0:java.lang.IllegalArgumentException
System.out.println(str.repeat(-2));
// 等于0:空白串("")
System.out.println(str.repeat(0));
// JavaJavaJava
System.out.println(str.repeat(3));
// java.lang.OutOfMemoryError
System.out.println(str.repeat(Integer.MAX_VALUE));
// 4
System.out.println("A\nB\nC\rD".lines().count());
String s1 = " hello java 1.8 ";
System.out.println(s1);
//.concat() 方法相當(dāng)于 + 屬性字符串連接
System.out.println("hello".concat("\s".repeat(30)).concat("java19"));
System.out.println("hello" + "\s".repeat(30) + "java19");
//清除首尾連續(xù)空格
System.out.println(s1.trim());
System.out.println(s1.strip());
System.out.println(s1.stripLeading());
System.out.println(s1.stripTrailing());
//清除所有空格
System.out.println(s1.replace(" ",""));System.out.println("------------");
System.out.println("".isBlank());
System.out.println("".isEmpty());
System.out.println(" ".isBlank());//true
System.out.println(" ".isEmpty());//false
System.out.println(" ".trim().length() == 0 ? "空字符串" : "正確");
System.out.println(" ".isBlank() ? "空字符串" : "正確");
System.out.println("*".repeat(60));
System.out.println("------------");
//求字符串中行數(shù)
System.out.println("hello\njava\nok\n123\nmysql");
System.out.println("hello\njava\nok\n123\nmysql".lines().count());
判斷一個(gè)對象是否屬于一個(gè)類可以用關(guān)鍵字instanceof,它是二元操作符,格式為:
對象 instanceof 類名
式子的值為一個(gè)布爾值(boolean)Object sth;
boolean isString = sth instanceof String;或者if (sth instanceof String) {
// your code
}字符串添加方法
序
號
方法描述
1 public StringBuffer append(String s) 將指定的字符串追加到此字符序列。
2 public StringBuffer reverse() 將此字符序列用其反轉(zhuǎn)形式取代。
3 public StringBuilder delete(int start, int end) 移除此序列的子字符串中的字符。
4 public insert(int offset, int i) 將 int 參數(shù)的字符串表示形式插入此序列中。
5 insert(int offset, String str) 將 str 參數(shù)的字符串插入此序列中。
6 replace(int start, int end, String str) 使用給定 String 中的字符替換此序列的子字符串
中的字符。1 int capacity() 返回當(dāng)前容量。
2 char charAt(int index) 返回此序列中指定索引處的 char 值。
3 void ensureCapacity(int minimumCapacity) 確保容量至少等于指定的最小值。
4
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 將字符從此序列復(fù)制到目
標(biāo)字符數(shù)組 dst 。
5 int indexOf(String str) 返回第一次出現(xiàn)的指定子字符串在該字符串中的索引。
6
int indexOf(String str, int fromIndex) 從指定的索引處開始,返回第一次出現(xiàn)的指定子字符
串在該字符串中的索引。
7 int lastIndexOf(String str) 返回最右邊出現(xiàn)的指定子字符串在此字符串中的索引。
8 int lastIndexOf(String str, int fromIndex) 返回 String 對象中子字符串最后出現(xiàn)的位置。
9 int length() 返回長度(字符數(shù))。
10 void setCharAt(int index, char ch) 將給定索引處的字符設(shè)置為 ch 。
11 void setLength(int newLength) 設(shè)置字符序列的長度。
12
CharSequence subSequence(int start, int end) 返回一個(gè)新的字符序列,該字符序列是此
序列的子序列。
13
String substring(int start) 返回一個(gè)新的 String ,它包含此字符序列當(dāng)前所包含的字符子
序列。
14
String substring(int start, int end) 返回一個(gè)新的 String ,它包含此序列當(dāng)前所包含的字
符子序列。
15 String toString() 返回此序列中數(shù)據(jù)的字符串表示形式。