ホームページ  >  記事  >  Java  >  Javaの文字列クラス

Javaの文字列クラス

WBOY
WBOYオリジナル
2024-08-30 15:42:54235ブラウズ

文字列は、java.lang を拡張した Java の最終クラスです。文字列として表現されるオブジェクト。 Serializable、Comparable、および CharSequence インターフェイスは文字列クラスによって実装されます。 「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: 2 つの文字列を追加します。

2 つの文字列を追加すると、結果は文字列の連結になります。

コード:

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の文字列クラス

説明:

  • まず、「I am string」として文字列 str を作成しました。次に、文字列の concat メソッドを使用して、文字列と連結します。
  • しかし、文字列は不変であるため、その値や状態は変更できません。連結された文字列はメモリの別の部分を取得します。
  • 参照 str は引き続き「I am string」を参照します。
  • 同じ文字列の値を更新するには、次の例に示すように、連結された文字列を特定の参照に割り当てる必要があります。

例 #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 には 2 種類の文字列クラスがあります。不変クラスである String クラスと、可変クラスである StringBuilder および StringBuffer。

StringBuilder と StringBuffer にはいくつかの違いがあります。

  • StringBuffer はスレッドセーフであり、同期されています。つまり、2 つのスレッドが同時に文字列バッファーのメソッドにアクセスできないことを意味します。
  • StringBuilder は非同期であり、スレッドセーフではありません。つまり、2 つのスレッドが文字列バッファーのメソッドに同時にアクセスできるということです。
  • 効率という点では、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の文字列クラス

Java では String クラスはどのように機能しますか?

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

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