ホームページ  >  記事  >  Java  >  Javaの文字列関数

Javaの文字列関数

PHPz
PHPzオリジナル
2024-08-30 15:32:46682ブラウズ

文字列で操作を実行するために Java で提供される多くのメソッドは、文字列関数と呼ばれます。メソッドは、compare()、concat()、equals()、split()、length()、replace()、compareTo() などです。 Java の文字列は定数であり、リテラルまたはキーワードを使用して作成されます。文字列リテラルは Java メモリを効率的にし、キーワードは通常のメモリに Java 文字列を作成します。文字列は文字値の配列を表し、クラスは Serializable、Comparable、CharSequence インターフェイスなどの 3 つのインターフェイスによって実装されます。これは、シリアル化または比較可能な方法で文字のシーケンスを表します。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

Java の文字列関数の概念

以下は Java の文字列関数の主な概念です:

1.文字列の作成

Java では、String オブジェクトを作成する主な方法が 2 つあります。

文字列リテラルの使用: Java で文字列リテラルを生成するには、二重引用符が使用されます。

例:

String s= "Hello World!";

new キーワードの使用: 「new」を使用して Java 文字列を作成できます。

例:

String s=new String ("Hello World!");

2.文字列の長さ

オブジェクトに関する情報を取得するために使用されるメソッドは、Java ではアクセサー メソッドと呼ばれます。文字列に関連するアクセサー メソッドの 1 つは、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());
}}

出力:

Javaの文字列関数

3.連結文字列

このメソッドは、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);
}}

出力:

Javaの文字列関数

4.フォーマット文字列の作成

printf() 関数と format() 関数は、フォーマットされた数値を出力します。この文字列には、format () という名前の同様のクラス メソッドがあります。 String オブジェクトが生成されます。ワンタイム印刷コマンドとは対照的に、String オブジェクトでアクセス可能な静的 format () メソッドを使用すると、再利用可能なフォーマット済み文字列の構築が可能になります。

Java の文字列関数のメソッド

次はさまざまな方法です:

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 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.

Examples of String functions in Java

In this section, we have discussed some examples of string functions in Java.

Example #1: Check if a string is empty

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:

Javaの文字列関数

Example #2: Trim whitespaces in a string

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:

Javaの文字列関数

Example #3: Convert a string to lowercase

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:

Javaの文字列関数

Example #4: Replace a part of a string

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:

Javaの文字列関数

Example #5: Check if two strings are equal

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:

Javaの文字列関数

Conclusion

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。