首頁  >  文章  >  Java  >  Java中的字串類

Java中的字串類

WBOY
WBOY原創
2024-08-30 15:42:54233瀏覽

字串是Java中擴展java.lang的最終類別。表示為字串的物件。 Serialized、Comparable 和 CharSequence 介面由 string 類別實作。所有字串文字(例如“string example”)都被視為 String 類別的實例。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

字串的工作

  • 如果你想要這樣的字串:我在孟買的「宮殿」。
  • 在這種情況下,java編譯器會誤解並拋出錯誤「我在孟買的「宮殿」。」
  • 會為編譯器帶來不明確的情況。
  • 為了處理這種情況,反斜線「」字元將派上用場。

代碼:

public class test
{
public static void main(String[] args)
{
String s1 = "I am at a \"Palace \" of Mumbai.";
System.out.println(s1);
}
}

輸出:

Java中的字串類

範例#1:新增兩個字串。

如果將兩個字串相加,結果將是字串連接。

代碼:

public class test
{
public static void main(String[] args)
{
int a = 10;
int b = 20;
int c = a+b;
System.out.println(c);
String s = "10";
int d = 10;
String s1 = s+d;
System.out.println(s1);
}
}

輸出:

Java中的字串類

範例#2:字串是不可變的。

字串一旦創建,其值就無法變更。字串是恆定的。這樣的屬性稱為不可變的。

代碼:

class Test
{
public static void main(String args[])
{
String str="I am string";
str.concat(" I am instance of String class.");
System.out.println(str);
}
}

輸出:

Java中的字串類

說明:

  • 首先,您建立了字串 str 作為「I am string」。然後藉助 string 的 concat 方法連接一些字串。
  • 但是由於字串是不可變的,因此它們的值或狀態無法變更。連接的字串將佔用另一部分記憶體。
  • 引用 str 仍然指「我是字串」。
  • 要更新相同字串的值,我們必須將連接的字串指派給特定引用,如下例所示。

範例#3

代碼:

class Test
{
public static void main(String args[])
{
String str="I am string";
str = str.concat(" I am instance of String class.");
System.out.println(str);
}
}

輸出:

Java中的字串類

要建立可變字串,可以使用 StringBuffer 或 StringBuilder 類別。

字串類別的型別

Java中有兩種類型的字元序列類別。不可變類別是 String 類,可變類別是 StringBuilder 和 StringBuffer。

StringBuilder 和 StringBuffer 有一些差別。

  • StringBuffer 是線程安全且同步的。這意味著沒有兩個執行緒可以同時存取字串緩衝區的方法。
  • StringBuilder 是非同步的且不是執行緒安全的。這意味著兩個執行緒可以同時存取字串緩衝區的方法。
  • 說到效率,StringBuilder 比 StringBuffer 更有效率。

下面給出的是 StringBuffer 和 StringBuilder 的範例

1.字串緩衝區

代碼:

class StringBufferExample
{
public static void main(String args[])
{
StringBuffer bufferstring=new StringBuffer("I am stringbuffer.");
bufferstring.append("I am thread safe.");
System.out.println(bufferstring);
}
}

輸出:

Java中的字串類

2.字串產生器

代碼:

class StringBuilderExample
{
public static void main(String args[])
{
StringBuilder builderstring=new StringBuilder("I am stringbuilder.");
builderstring.append("I am more efficient than string buffer.");
System.out.println(builderstring);
}

輸出:

Java中的字串類

String 類別在 Java 中如何運作?

在Java中,字串是字元的序列,與c、c++不同,它們是String類別的物件。

此類別支援一些建構函數,如下:

Java 中重要的字串建構子

下面給出了java中一些重要的字串建構子:

  • String(): 它用空資料初始化一個字串物件。
  • String(byte[] bytes): 它透過解碼位元組陣列並將其指派給已建立的參考來建構字串。
  • String(char[] value): 它透過提供的字元陣列建構一個字串,並將其指派給已建立的參考。

Java 中重要的字串方法

下面給了java中一些重要的字串方法:

  • charAt(int index): It returns the character present at the given index in a string.
  •  concat(String s): It concatenates the existing string with the string provided as an argument.
  • equals(Object anObject): Compares this string to the given object and return boolean.
  • substring(int beginIndex, int endIndex): It returns a string, i.e. substring of this string.
  • toLowerCase(): Converts a string to lower case.
  • toUpperCase(): Converts a string to upper case.
  • startsWith(String prefix): Tests if the string starts with the prefix provided as an argument.
  • trim(): It trims all leading and trailing whitespaces from the string.

Examples of String Class in Java

Given below are the examples of String Class in Java:

Example #1

Let us check whether the string contains only whitespaces.

Code:

class StringWhitespaceChecker
{
public static boolean isStringWithWhitespace(String s)
{
if(s.trim().isEmpty)
{
return true;
}
else
{
return false;
}
}
public static void main(String args[])
{
StringWhitespaceChecker s1 = new StringWhitespaceChecker();
String s =new String(" ");
String string = new String(" I am string. ");
boolean condition1 = s1.isStringWithWhitespace(s);
boolean condition2 = s1.isStringWithWhitespace(string);
System.out.println(condition1);
System.out.println(condition2);
}
}

Output:

Java中的字串類

Explaination:

  • The function first trims all the whitespaces and then checks whether it is empty; if it is so, it will return true or false, respectively.

Example #2

String function.

Code:

class Test
{
public static void main(String args[])
{
String s = new String("hello");
s.toUpperCase();
System.out.println(s);
s = s.concat("world");
System.out.println(s);
System.out.println(s.charAt(1));
String s1 = new String("helllo");
boolean b = s.equals(s1);
System.out.println(b);
}
}

Output:

Java中的字串類

Example #3

Java String to toCharArray()

Code:

class Test
{
public static void main(String args[])
{
String s = new String("hello");
Char[] c1 = s.toCharArray()
for(int i=0;i<c1.length;i++)
{
System.out.println(c1[i]);
}
}
}

Output:

Java中的字串類

Conclusion

This article is focused upon the criteria such as introduction to the string, String constructors, String Methods, Types of String classes, String immutable, StringBuffer and StringBuilder. Examples of each property. A string is a very important class in Java. You will work with string manipulation in multiple ways in real-world projects.

以上是Java中的字串類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn