Java String类

本文介绍了Java中创建和操作String对象的方法,包括直接赋值、使用构造函数、计算字符串长度、字符串连接以及格式化字符串。String类是不可变的,推荐使用StringBuilder或StringBuffer进行大量修改。此外,文章列举了多个String类的重要方法,如charAt(),equals(),length(),split()等。
摘要由CSDN通过智能技术生成
  1. 创建字符串创建字符串的最直接方法是 -
    String str = “Hello world!”;
    Java
    每当它在代码中遇到字符串文字时,编译器就会创建一个String对象,在本例中str对象的值为Hello world!。
    与其他对象一样,可以使用new关键字和构造函数来创建String对象。String类有11个构造函数,方便使用不同的源(例如:字符数组)提供字符串的初始值。
    示例
    public class StringDemo {

    public static void main(String args[]) {
    char[] helloArray = { ‘Y’, ‘i’, ‘i’, ‘b’, ‘a’, ‘i’ };
    String helloString = new String(helloArray);
    System.out.println( helloString );
    }
    }
    Java
    执行上面示例代码,得到下结果:
    Yiibai
    Shell

注 - String类是不可变的,因此一旦创建,就无法更改String对象。 如果想要对字符串进行大量修改,则应使用StringBuffer和StringBuilder。

  1. 字符串长度用于获取对象信息的方法称为访问器方法。 可以与字符串一起使用来获取字符串长度的一个访问器方法是length()方法,它返回字符串对象中包含的字符数。
    以下程序是String类的length()方法的示例。
    public class StringDemo {

    public static void main(String args[]) {
    String greeting = “Hi,Welcome to Yiibai.com”;
    int len = greeting.length();
    System.out.println( greeting+" 字符串的长度是: " + len );
    }
    }
    Java
    执行上面示例代码,得到下结果:
    Hi,Welcome to Yiibai.com 字符串的长度是: 24
    Shell

  2. 连接字符串String类包含一个用于连接两个字符串的方法 -
    string1.concat(string2);
    Java
    这将返回一个新字符串:string1,并且string1在结尾处添加了string2。 还可以将concat()方法与字符串文字一起使用,例如 -
    "My name is “.concat(“Maxsu”);
    Java
    字符串通常使用+运算符连接,如 -
    “Hello,” + " world” + “!”
    Java
    上面代码执行后得到的结果是:
    “Hello, world!”
    Shell
    下面再来看另一个例子 -
    public class StringDemo {

    public static void main(String args[]) {
    String string1 = “Bai”;
    System.out.println(“Yii” + string1 + “.com”);
    }
    }
    Java
    上面代码执行后得到的结果是:
    YiiBai.com
    Shell

  3. 创建格式化字符串Java中使用printf()和format()方法来打印带有格式化数字的输出。 String类有一个等效的类方法format(),它返回一个String对象而不是一个PrintStream对象。
    使用String的static format()方法可以创建重用的格式化字符串,而不是一次性打印语句。 例如 -
    System.out.printf("The value of the float variable is " +
    "%f, while the value of the integer " +
    "variable is %d, and the string " +
    “is %s”, floatVar, intVar, stringVar);
    Java
    上面打印语句可使用格式化写为:
    String fs;
    fs = String.format("The value of the float variable is " +
    "%f, while the value of the integer " +
    "variable is %d, and the string " +
    “is %s”, floatVar, intVar, stringVar);
    System.out.println(fs);
    Java

  4. String类方法以下是String类定义的方法列表 -

编号
方法
描述

1
char charAt(int index)
返回指定索引处的字符。

2
int compareTo(Object o)
将此String对象与另一个对象进行比较。

3
int compareTo(String anotherString)
按字典顺序比较两个字符串。

4
int compareToIgnoreCase(String str)
按字典顺序比较两个字符串,但不区分大小写。

5
String concat(String str)
将指定的字符串连接到此字符串的末尾。

6
boolean contentEquals(StringBuffer sb)
当且仅当此String表示的字符串与指定的StringBuffer相同的字符序列时,才返回true。

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编码为字节序列,将结果存储到新的字节数组中。

13
byte[] getBytes(String charsetName)
使用指定的字符集将此String编码为字节序列,将结果存储到新的字节数组中。

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)
将替换此字符串中匹配给定正则表达式的每个子字符串。

31
String replaceFirst(String regex, String 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)
返回传递的数据类型参数的字符串表示形式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海澜明月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值