Java.io包教程

Java - 字符串类

在Java编程中广泛使用的字符串是一系列字符.在Java编程语言中,字符串被视为对象.

Java平台提供String类来创建和操作字符串.

创建字符串

创建字符串的最直接方法是写入和减去;

String greeting = "Hello world!";

每当遇到代码中的字符串文字时,编译器就会创建一个String对象,其值为"Hello world!".

与任何其他对象一样,您可以使用new关键字和构造函数创建String对象.String类有11个构造函数,允许您使用不同的源提供字符串的初始值,例如作为一个字符数组.

示例

public class StringDemo {

   public static void main(String args[]) {
      char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
      String helloString = new String(helloArray);  
      System.out.println( helloString );
   }
}

这将产生以下结果 :

输出

hello.

注意 :  String类是不可变的,因此一旦创建,就无法更改String对象.如果有必要对字符串进行大量修改,那么你应该使用 String Buffer&字符串生成器类.

字符串长度

用于获取对象信息的方法称为访问器方法.可以与字符串一起使用的一种访问器方法是length()方法,它返回字符串对象中包含的字符数.

以下程序是长度的示例(),方法String类.

示例

public class StringDemo {

   public static void main(String args[]) {
      String palindrome = "Dot saw I was Tod";
      int len = palindrome.length();
      System.out.println( "String Length is : " + len );
   }
}

这将产生以下结果 :

输出

String Length is : 17

连接字符串

String类包括一个连接两个字符串的方法 :

string1.concat(string2);

这将返回一个新字符串,该字符串为string1,并在结尾处添加了string2.你也可以使用带有字符串文字的concat()方法,如 :

"My name is ".concat("Zara");

字符串通常与&plus连接;运算符,如& - ;

 
"Hello,"+"world"+"!"

导致 :

"Hello, world!"

让我们看一下以下示例 :

示例

public class StringDemo {

   public static void main(String args[]) {
      String string1 = "saw I was ";
      System.out.println("Dot " + string1 + "Tod");
   }
}

这将产生以下结果 :

输出

Dot saw I was Tod

创建格式字符串

你有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);

你可以写 :

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);

字符串方法

以下是String class :

$ b $支持的方法列表b

Sr.No.方法&说明
1char charAt(int index)

返回指定索引处的字符.

2int compareTo(Object o)

将此String与另一个Object进行比较.

3int compareTo(String anotherString)

按字典顺序比较两个字符串.

4int compareToIgnoreCase(String str)

按字典顺序比较两个字符串,忽略大小写差异.

5String concat(String str)

将指定的字符串连接到此字符串的末尾.

6boolean contentEquals(StringBuffer sb)

当且仅当此String表示与指定的StringBuffer相同的字符序列时,返回true./p>

7static String copyValueOf(char [] data)

返回表示指定数组中字符序列的String .

8static String copyValueOf(char [] data,int offset,int count)

返回一个String代表着特征指定数组中的r序列.

9boolean endsWith(String suffix)

测试此字符串是否以指定的后缀.

10boolean equals(Object anObject)

将此字符串与指定对象进行比较.

11boolean equalsIgnoreCase(String anotherString)

将此String与另一个String进行比较,忽略大小写.

12byte getBytes()

使用平台的默认值将此String编码为字节序列t charset,将结果存储到新的字节数组中.

13byte [] getBytes(String charsetName)

使用命名的charset将此String编码为字节序列,将结果存储到新的字节数组中.

14void getChars(int srcBegin,int srcEnd, char [] dst,int dstBegin)

将此字符串中的字符复制到目标字符数组中.

15int hashCode()

返回此字符串的哈希码.

16int indexOf(int ch)

返回指定字符第一次出现的字符串中的索引.

17int indexOf(int ch,int fromIndex)

返回指定字符第一次出现的字符串中的索引,从指定索引处开始搜索.

18int indexOf(String str)

返回指定子字符串第一次出现的字符串中的索引.

19int indexOf(String str,int fromIndex)

从指定的子字符串开始,返回指定子字符串第一次出现的字符串中的索引index.

20String intern()

返回字符串对象的规范表示.

21int lastIndexOf(int ch)

返回指定字符最后一次出现的字符串中的索引.

22int lastIndexOf(int ch,int fromIndex)

返回指定字符最后一次出现的字符串中的索引,从指定的索引开始向后搜索.

23int lastIndexOf(String str)

返回指定子字符串最右边出现的字符串中的索引.

24int lastIndexOf(String str, int fromIndex)

返回指定子字符串最后一次出现的字符串中的索引,从指定索引开始向后搜索.

25int length()

返回此字符串的长度.

26布尔匹配(字符串正则表达式)

判断此字符串是否与给定的正则表达式匹配.

27boolean regionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len)

测试两个字符串区域是否相等.

28boolean regionMatches(int toffset,String other,int ooffset,int len)

测试两个字符串区域是否相等.

29字符串替换(char oldChar,char newChar)

返回一个新字符串,该字符串是用newChar替换此字符串中出现的所有oldChar而得到的.

30字符串replaceAll(String regex,String replacement

替换此字符串中与给定re匹配的每个子字符串具有给定替换的gular表达式.

31String replaceFirst(String regex,String replacement)

替换第一个子字符串此字符串与给定的正则表达式匹配给定的替换.

32String [] split(String regex)

将此字符串拆分为给定正则表达式的匹配项.

33String [] split(String regex,int limit)

围绕给定正则表达式的匹配拆分此字符串.

34boolean startsWith(String prefix)

测试此字符串是否以指定的前缀开头.

35boolean startsWith(String prefix,int toffset)

测试此字符串是否以指定的索引开头指定的前缀.

36CharSequence subSequence(int beginIndex,int endIndex)

返回一个新的字符序列,它是该序列的子序列.

37String substring(int beginIndex)

返回一个新字符串,它是该字符串的子字符串.

38String substring(int beginIndex,int endIndex)

返回一个新字符串,它是该字符串的子字符串.

39char [] toCharArray()

将此字符串转换为新的字符数组.

40String toLowerCase()

使用默认语言环境的规则将此String中的所有字符转换为小写.

41String toLowerCase(区域设置区域设置)

使用给定区域设置的规则将此String中的所有字符转换为小写.

42String toString()

这个对象(已经是一个字符串!)本身就返回了.

43String toUpperCase()

使用默认语言环境的规则将此String中的所有字符转换为大写.

44字符串toUpperCase(区域设置区域设置)

使用给定语言环境的规则将此String中的所有字符转换为大写.

45String trim()

返回字符串的副本,省略前导和尾随空格.

46static String valueOf(原始数据类型x)

返回传递的数据类型参数的字符串表示形式.