Java 中提供的許多用於在字串中執行操作的方法稱為字串函數。方法有compare()、concat()、equals()、split()、length()、replace()、compareTo()等。 Java 中的字串是常數,並使用文字或關鍵字建立。字串字面量使 Java 記憶體變得高效,並且該關鍵字在普通記憶體中建立 Java 字串。字串表示字元值數組,該類別由Serialized、Comparable、CharSequence介面等三個介面實作。它以序列化或可比較的方式表示字元序列。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗以下是java中字串函數的主要概念:
在 Java 中,建立 String 物件有兩種主要方法:
使用字串文字: 雙引號用於在 Java 中產生字串文字。
範例:
String s= "Hello World!";
使用new關鍵字:可以使用「new」建立Java字串。
範例:
String s=new String ("Hello World!");
用於取得物件資訊的方法在 Java 中稱為存取器方法。與字串相關的一種此類存取器方法是 length() 方法。這將傳回字串物件中的字元數。
public class Exercise { public static void main(String args[]){ String s1="Hello"; String s2="World"; System.out.println("string length is: "+s1.length()); System.out.println("string length is: "+s2.length()); }}
輸出:
該方法傳回一個新字串,即 string1 和 string2 組合在末尾。 Concat() 方法可以與字串文字一起使用來完成此操作。字串通常也使用 + 運算子連接。
public class ExerciseNew { public static void main(String args[]){ String s1="Hello"; s1=s1.concat("What is your good name?"); System.out.println(s1); }}
輸出:
printf() 和 format() 函數輸出格式化的數字。字串有一個類似的類別方法,名為format()。它產生一個 String 物件。與一次性列印命令相比,String 物件中可存取的靜態 format() 方法允許建構可重複使用的格式化字串。
以下是不同的方法:
Method | Description |
char charAt(int index) | It returns the char value of the particular index as mentioned. |
int length() | It returns the length of the string |
static String format(String format, Object… args) | It returns a string that is duly formatted. |
static String format(Locale l, String format, Object… args) | It returns a formatted string along with the given locale. |
String substring(int beginIndex) | It returns the substring, which starts from begin index. |
String substring(int beginIndex, int endIndex) | It returns a substring for a given start index position and ends index. |
boolean contains(CharSequence s) | It returns true or false after doing a match between the sequence of char values. |
static String join(CharSequence delimiter, CharSequence… elements) | It returns a string that is joined |
static String join(CharSequence delimiter, Iterable extends CharSequence> elements) | It returns a joined string, the same as above. |
boolean equals(Object another) | It checks the equality of the string. It does so with the given object. |
boolean isEmpty() | It checks if a given string is empty or not. |
String concat(String str) | It concatenates the specified string like the above example. |
String replace(char old, char new) | It replaces all occurrences of the specified old char value. With new value. |
String replace(CharSequence old, CharSequence new) | It replaces all occurrences of the given specified CharSequence with the new one. |
static String equalsIgnoreCase(String another) | It compares with another string, but It is not case-sensitive. |
String[] split(String regex) | It returns a split string based on matching the regex. |
String[] split(String regex, int limit) | It returns a split string that matches regex and limit. |
String intern() | It returns a string that is interned. |
int indexOf(int ch) | It returns the selected char value index. |
int indexOf(int ch, int fromIndex) | It returns the specified char value index, which starts with a given index. |
int indexOf(String substring) | It returns the selected substring index. |
int indexOf(String substring, int fromIndex) | It returns the selected substring index, which starts with a given index. |
String toLowerCase() | It returns a string with all chars in lowercase. |
String toLowerCase(Locale l) | It returns a string in lowercase with a specified locale. |
String toUpperCase() | It returns a string with all chars in uppercase. |
String toUpperCase(Locale l) | Same as above but with a specified locale. |
String trim() | It removes the starting and ending whitespaces of this string. |
static String valueOf(int value) | It converts another data type into a string. It is called an overloaded method. |
In this section, we have discussed some examples of string functions in Java.
Code:
public class IsEmptyExercise{ public static void main(String args[]){ String s1=""; String s2="Hello"; System.out.println(s1.isEmpty()); // true System.out.println(s2.isEmpty()); // false }}
Output:
Code:
public class StringTrimExercise{ public static void main(String args[]){ String s1=" HelloWorld "; System.out.println(s1+"How are you doing today"); // without trim() System.out.println(s1.trim()+"How are you doing today"); // with trim() }}
Output:
Code:
public class StringLowerExercise{ public static void main(String args[]){ String s1="HELLO HOW Are You TODAY?"; String s1lower=s1.toLowerCase(); System.out.println(s1lower);} }
Output:
Code:
public class ReplaceExercise{ public static void main(String args[]){ String s1="hello how are you today"; String replaceString=s1.replace('h','t'); System.out.println(replaceString); }}
Output:
Code:
public class EqualsExercise{ public static void main(String args[]){ String s1="Hi"; String s2="Hey"; String s3="Hello"; System.out.println(s1.equalsIgnoreCase(s2)); // returns true System.out.println(s1.equalsIgnoreCase(s3)); // returns false } }
Output:
Apart from the above-mentioned characteristics, functions, and methods, there are other facts with the String class. The string class is a final class, which is why String class objects are immutable in nature. JVM reserves a special memory area for string classes; this area is called the String constant pool.
In the String library, available with Java. Lang, overriding the String references are possible, but the content or literals cannot be copied. Any number closed in double quotes is also treated as a string.
Students should test these codes in an IDE and modify them to enhance their understanding further. String manipulation is very important to know in any programming language, and developers use it daily.
以上是Java 中的字串函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!