日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java学习笔记之:Java String类

發布時間:2023/12/2 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java学习笔记之:Java String类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、引言

字符串廣泛應用在Java編程中,在Java中字符串屬于對象,Java提供了String類來創建和操作字符串。

創建字符串最簡單的方式如下:

String str= "Hello world!";

String類型是特殊的引用類型,我們也可以通過實例化的方式來創建

String string=new String("ABC"); System.out.println(string);

二、string方法

下面是String類支持的方法,下面是String類支持的方法。

SN(序號)

方法描述

1

char charAt(int index)
返回指定索引處的 char 值。

2

int compareTo(Object o)
把這個字符串和另一個對象比較。

3

int compareTo(String anotherString)
按字典順序比較兩個字符串。

4

int compareToIgnoreCase(String str)
按字典順序比較兩個字符串,不考慮大小寫。

5

String concat(String str)
將指定字符串連接到此字符串的結尾。

6

boolean contentEquals(StringBuffer sb)
當且僅當字符串與指定的StringButter有相同順序的字符時候返回真。

7

static String copyValueOf(char[] data)
返回指定數組中表示該字符序列的 String

8

static String copyValueOf(char[] data, int offset, int count)
返回指定數組中表示該字符序列的 String

9

boolean endsWith(String suffix)
測試此字符串是否以指定的后綴結束。

10

boolean equals(Object anObject)
將此字符串與指定的對象比較。

11

boolean equalsIgnoreCase(String anotherString)
將此 String 與另一個 String 比較,不考慮大小寫。

12

byte[] getBytes()
?
使用平臺的默認字符集將此 String 編碼為 byte 序列,并將結果存儲到一個新的 byte 數組中。

13

byte[] getBytes(String charsetName)
使用指定的字符集將此 String 編碼為 byte 序列,并將結果存儲到一個新的 byte 數組中。

14

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
將字符從此字符串復制到目標字符數組。

15

int hashCode()
返回此字符串的哈希碼。

16

int indexOf(int ch)
返回指定字符在此字符串中第一次出現處的索引。

17

int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出現指定字符處的索引,從指定的索引開始搜索。

18

int indexOf(String str)
?
返回指定子字符串在此字符串中第一次出現處的索引。

19

int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出現處的索引,從指定的索引開始。

20

String intern()
?
返回字符串對象的規范化表示形式。

21

int lastIndexOf(int ch)
?
返回指定字符在此字符串中最后一次出現處的索引。

22

int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最后一次出現處的索引,從指定的索引處開始進行反向搜索。

23

int lastIndexOf(String str)
返回指定子字符串在此字符串中最右邊出現處的索引。

24

int lastIndexOf(String str, int fromIndex)
?
返回指定子字符串在此字符串中最后一次出現處的索引,從指定的索引開始反向搜索。

25

int length()
返回此字符串的長度。

26

boolean matches(String regex)
告知此字符串是否匹配給定的正則表達式。

27

boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
測試兩個字符串區域是否相等。

28

boolean regionMatches(int toffset, String other, int ooffset, int len)
測試兩個字符串區域是否相等。

29

String replace(char oldChar, char newChar)
返回一個新的字符串,它是通過用 newChar 替換此字符串中出現的所有 oldChar 得到的。

30

String replaceAll(String regex, String replacement
使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。

31

String replaceFirst(String regex, String replacement)
?
使用給定的 replacement 替換此字符串匹配給定的正則表達式的第一個子字符串。

32

String[] split(String regex)
根據給定正則表達式的匹配拆分此字符串。

33

String[] split(String regex, int limit)
根據匹配給定的正則表達式來拆分此字符串。

34

boolean startsWith(String prefix)
測試此字符串是否以指定的前綴開始。

35

boolean startsWith(String prefix, int toffset)
測試此字符串從指定索引開始的子字符串是否以指定前綴開始。

36

CharSequence subSequence(int beginIndex, int endIndex)
?
返回一個新的字符序列,它是此序列的一個子序列。

37

String substring(int beginIndex)
返回一個新的字符串,它是此字符串的一個子字符串。

38

String substring(int beginIndex, int endIndex)
返回一個新字符串,它是此字符串的一個子字符串。

39

char[] toCharArray()
將此字符串轉換為一個新的字符數組。

40

String toLowerCase()
使用默認語言環境的規則將此 String 中的所有字符都轉換為小寫。

41

String toLowerCase(Locale locale)
?
使用給定 Locale 的規則將此 String 中的所有字符都轉換為小寫。

42

String toString()
?
返回此對象本身(它已經是一個字符串!)。

43

String toUpperCase()
使用默認語言環境的規則將此 String 中的所有字符都轉換為大寫。

44

String toUpperCase(Locale locale)
使用給定 Locale 的規則將此 String 中的所有字符都轉換為大寫。

45

String trim()
返回字符串的副本,忽略前導空白和尾部空白。

46

static String valueOf(primitive data type x)
返回給定data type類型x參數的字符串表示形式。

三、筆記

package com.hgd.study6.other;import java.awt.CardLayout;import javax.print.DocFlavor.BYTE_ARRAY;/*** string: 存在于常量值,字符換不可變,如果對字符串進行操控,將返回一個新的字符串而字符串本身是不變的* @author HuTiger**/ public class StringTest {public static void main(String[] args) {// String string=new String("ABC"); // System.out.println(string); String str = "ABCD";// 返回索引處的char值System.out.println(str.charAt(0));// 返回字符串長度 System.out.println(str.length());// 返回指定索引處的字符(unicode碼)System.out.println(str.codePointAt(0));// 按字典順序比較兩個字符String s1 = "A";String s2 = "BA";System.out.println(s2.compareTo(s1));// 將指定字符串連接到此字符串的結尾 System.out.println(s2.concat(s1));// 包含 System.out.println(s2.contains(s1));// 測試此字符串是否 以指定的后綴結束 ,可以用來判斷文件的后綴名String musicString = "菊花臺.MP3";System.out.println(musicString.endsWith("MP3"));// 返回字符串的字節數,流用的比較多String strbyte = "ABCD";byte[] bsBytes = str.getBytes();for (int i = 0; i < bsBytes.length; i++) {System.out.println(bsBytes[i]);}// 獲取哈希碼String strhash = "A";System.out.println(strhash.hashCode());// indexOf(整型) 入參是對應字符的Unicode碼 返回的是index索引,如果不存在返回-1String indexstring = "ABCD";System.out.println(indexstring.indexOf(97));// 返回的時候字符串出點的縮影的開始位置System.out.println(indexstring.indexOf("BC"));// 指定位置開始的字符串System.out.println(indexstring.indexOf("D", 3));//返回一個新的字符串,它是通過用 newChar 替換此字符串中出現的所有 oldChar 得到的。String sreplaceString = "asdasfawgsa";System.out.println(sreplaceString.replace('a', 'A'));System.out.println(sreplaceString.replace("sf", "SF"));// 截取字符串String subString = "ABCDEFG";System.out.println(subString.substring(1));System.out.println(subString.substring(1, 3));// 包含前面的不包含后面//轉換為char數組String chararraystring = "同志們辛苦了";char[] cs = chararraystring.toCharArray();for (int i = 0; i < cs.length; i++) {System.out.println(cs[i]);}//去除空格String trimString=" admin ";System.out.println(trimString.trim());//字符串轉換int age=30;System.out.println(age+1);String string=String.valueOf(age);} } View Code

?

轉載于:https://www.cnblogs.com/huguodong/p/5892634.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Java学习笔记之:Java String类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。