Stringクラスの基本的な使い方

5861 ワード


文字列の使い方
//バイト配列による文字列の作成
byte[] by = { 'a', 'b', 'c', 'd' };
String newByteString = new String(by);

 
 
1,length()取得文字列の長さ
 
//「バイト配列に基づいて文字列を作成する長さは
int len = newByteString.length();
System.out.println("               :" + len);

 
 
    2,codePointAt(int index)
//指定したインデックスの文字(Unicodeコードポイント)を返します.
int codePointat = newByteString.codePointAt(2);
System.out.println("          (Unicode    )" + codePointat);

 
 
       3,codePointBefore(int index)
//指定したインデックスの前の文字の符号化(Unicodeコードポイント)を返す
int codePointatbefore = newByteString.codePointBefore(2);
System.out.println("               (Unicode    )" +            codePointatbefore);

 
 
    4,codePointCount(int beginIndex, int endIndex)
//このStringの指定したテキスト範囲のUnicodeコード点数を返します.
 
   int codePointcount = newByteString.codePointCount(0, 3);
      System.out.println("    String           Unicode     。"
      + codePointcount);

 
 
   5,toLowerCase()
//文字列を小文字にする
String tolower = newByteString.toLowerCase();
System.out.println("       " + tolower);

 
   6,toUpperCase();
//文字列を大文字にする
String toUpper = newByteString.toUpperCase();
System.out.println("       " + toUpper);

 
 
 
   7,substring(int beginIndex)
//切り取り文字列、切り取りの開始位置を入力
String sub = newByteString.substring(0);
System.out.println("        " + sub);

 
 
  8,substring(int beginIndex,  int endIndex)
//切り取りの開始位置と終了位置で0<=subStratEnd<2
String subStratEnd = newByteString.substring(0, 2);
System.out.println("            " + subStratEnd);

 
 
    9, subSequence(int beginIndex,  int endIndex)
//このシーケンスのサブシーケンスである新しい文字シーケンスを返します.この用法はsubstring(0,2)と似ている.
CharSequence Charsequence = newByteString.subSequence(0, 2);
System.out.println("          " + Charsequence);

 
 
     10,startsWith( String prefix)
 
//この文字列が指定した文字で始まるかどうかをテストtrueはfalse No
boolean b= newByteString.startsWith("a");
System.out.println(b);

 
 
  11,startsWith( String prefix,  int toffset)
//指定した索引から始まるサブストリングが指定した接頭辞で始まるかどうかをテストします
String str ="a";
boolean c = newByteString.startsWith(str,0);
System.out.println("          "+c);

 
 
    12,indexOf(int ch)
//文字列に最初に出現した文字の位置をテストする
int indexof =  newByteString.indexOf("b");
System.out.println("                  "+indexof);

 
 
   13, indexOf( String str)
//str文字列の中の文字がnewByteString文字列で初めて現れる位置をテストする
int index =  newByteString.indexOf(str);
System.out.println(index);

 
 
   14,trim()
//先頭空白と末尾空白を無視して、この文字列の内容を返します.
String Trim=newByteString.trim();
System.out.println(Trim);

 
 
 
15文字列が空のisEmpty()であるか否かを判定する.
 
16文字列を文字に変換するtoCharArray()
 
17,文字列内のバイトbyte[]getBytes()を取得
 
18,2つの文字列が等しいかどうかを比較する(Object anObject)
 
 
Stringオブジェクトの作成
byte[] by = { 'a', 'b', 'c', 's' };
  //String()
//新しく作成したStringオブジェクトを初期化し、空の文字列を表すようにします.
//    String string = new String();
 
//String(byte[] bytes)
//プラットフォームのデフォルト文字セットを使用して指定したbyte配列を復号することで、新しいStringを構築します.
//String string = new String(by);
 
 
//String(byte[] bytes, int offset, int length)
//プラットフォームのデフォルト文字セットを使用して指定したbyteサブ配列を復号することで、新しいStringを構築します.
//String newString = new String(by, 1, 2);
//System.out.println(newString);//bc
 
     
  
Stringでよくある質問:
 
1、文字列が等しい
     	 String a = new String("Java");
String b = new String("Java");
System.out.println(a==b);
String aa = "Java";
String bb ="Java";
System.out.println(aa ==bb);

 
結果:
false newから出てきた2つのアドレス
true変数は定数プールに置かれたaaであり,bbはこのアドレスを参照する.
 
2,文字列相+の問題
 
   
 String aa = "java";
    String bb = "c";
    String dd = "javac";
    String cc = aa+bb;
    System.out.println(cc==dd); false  

false
    String aa = "java";
    String bb = "c";
    String dd = "aabb";
    String cc = "aa"+"bb";
    System.out.println(cc==dd);

 
 
true