デジタル学習Javaの文字列

Javaの学習がどの類似性を整理how2jに提供された情報に基づいており、それは正常です。

ラッパークラス

すべての基本的なタイプは、パッケージには、対応するクラスがあります。

デジタルラッパークラス

デジタルラッパークラスは、バイト、ショート、整数、ロング、フロート、ダブルがあり、これらのクラスは抽象クラス、ワード数です。クラスはカプセル化int型の整数であるような、Integerクラスはカプセル化と呼ばれています。

どのように基本的なタイプのパッケージのクラスに。

public class TestNumber{
    public static void main(String[] args){
        int i = 1;
        //基本类型转换为封装类
        Integer it = new Integer(i);
    }
}

どのようにラッパークラスの基本的な種類に

public class TestNumber{
    public static void main(String[] args){
        int i = 1;
        Integer it = new Integer(i);
        //封装类转换为基本类型
        int i2 = it.intValue();
    }
}

Shajiao自動ボクシングとアンボクシングいます。すなわち、上述した梱包方法は、コンストラクタを呼び出すようにされていません、Integer型は自動的=によって変換することができます。

public class TestNumber{
    public static void main(String[] args){
        int i = 2;
        //自动装箱
        Integer it = i;
    }
}

その自動開梱、整数intValueメソッドを呼び出す必要はありません。自動的= int型で変換され、開梱と呼ばれます。

public class TestNumber{
    public static void main(String[] args){
        Integer it = 5;
        //自动拆箱
        int i = it;
    }

比較的共通のInt最大値と最小値とはInteger.MIN_VALUEのInteger.MAX_VALUEのを呼び出すことであります。

public class TestNumber{
    public static void main(String[] args){
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
    }
}

演習自動ボクシングとアンボクシング

byte a = 3;
short b = 3;
float c = 3;
double d = 3;
//byte自动装箱和拆箱
Byte az=new Byte(a); 或 Byte az = a;
byte ac=az.byteValue(); 或 byte ac = az;
//short自动装箱和拆箱
Short bz = new Short(b);或 Short bz = b;
short bc = bz.shortValue();或short bc = bz;

デジタル文字列をオンにする方法は?我々はまだこの問題を検討しています。2つの方法があります。

方法1. 使用String的静态方法valueOf
方法2. 使把基本类型装箱为对象,然后调用对象的toString

public class TestNumber{
    public static void main(String[] args){
        int i = 5;
        
        //方法1
        String str = String.valueOf(i);
        System.out.pringln(str);
        //方法2
        Integer i2 = i;
        String str2 = Integer.toString(i2);
        System.out.println(str2);
    }
}

数字の文字列は、それを回しますか?

调用Integer的parseInt方法

public class TestNumber{
    public static void main(String[] args){
        String str = '999';
        //调用Integer的parseInt方法
        Integer i = Integer.parseInt(str);
        System.out.println(i);
    }
}

文字列にフロートは3.14「3.14」の文字列に「3.14」3.14をfloatに変換すること

public class TestNumber{
    public static void main(Strign[] args){
        float f = 3.14;
        //使用方法一
        String str = String.valueOf(f);
        //使用方法二
        Float f1 = f;
        String str1 = Float.toString(f1);
        Float f2 = Float.parseFloat(str1);
    }
}

Javaはまた、いくつかの数学的方法を提供し、それはあなたが直接呼び出すことができることを意味し、静的メソッドの形で存在します。私はいくつかの一般的なを持っています。

public class TestNumber{
    public static void main(String[] args){
        float f1 = 5.4f;
        float f2 = 5.5f;
        
        //5.4四舍五入到5
        System.out.println(Math.round(f1));
        
        //5.5四舍五入到6
        System.out.pringln(Math.round(f2));
        
        //得到0-1之间的随机浮点数(取不到1)
        System.out.println(Math.random());
        
        //得到0-10之间的随机数(取不到10),这个比较常用
        System.out.println((int)(Matn.random()*10));
        
        //开方
        System.out.println(Math.sqrt(9));
        
        //次方
        System.out.println(Math.pow(2,4));
        
        //派
        System.out.println(Math.PI);
        
        //自然常数
        System.out.println(Math.E);
    }
}

次に、どのように出力をフォーマットしますか?

%s 表示字符串
%d 表示数字
%n 表示换行

コードは示しています。次のように

public class TestNumber{
    public static void main(String[] args){
        String name = "小明";
        int count = 3;
        String title = "一等奖";

        //直接使用+进行字符串连接
        String sentence = name + "获得"+count+"次"+title;
        System.out.println(sentence);
        //使用格式化输出
        String sentence1 = "%s获得%d次%s";
        System.out.printf(sentence1,name,count,title);
    }
}

printfのフォーマットを使用し、まったく同じ効果を得ることができます

public class TestNumber{
    public static void main(String[] args){
        String name = "小明";
        int count = 3;
        String title = "一等奖";

        //直接使用+进行字符串连接
        String sentence = name + "获得"+count+"次"+title;
        System.out.println(sentence);
        //使用格式化输出
        String sentence1 = "%s获得%d次%s";
        System.out.format(sentence1,name,count,title);
    }
}

文字

文字タイプパッケージタイプ文字。

public class TestChar{
    public static void main(String[] args){
        char c1 = '1';
        //装箱
        Character c = c1; 
        //拆箱
        char c2 =c;
    }
}

次のカテゴリの文字をchar型の一般的な方法でラッパークラス。

Character.isLetter('a') //判断是否为字母
Character.isDigit('a') //判断是否数字
Character.isWhitespace(' ')//判断是否为空白
Character.isUpperCase('a') //判断是否为大写
Character.isLowerCase('a') //判断是否为小写
Character.toUpperCase('a') //转换为大写
Character.toLowerCase('A') //转换为小写 

文字列に文字タイプ。

public class TestChar{
    public static void main(String[] args){
        //下面的部分会报错,char无法转换成String
        String a = 'a';
        System.out.println(a);
        //可修改成如下
        String a = Character.toString('a');
        System.out.println(a);
    }
}

エラーになり、エラーは次のとおりです。

TestChar.java:4: 错误: 不兼容的类型: char无法转换为String
        String a = 'a';
                   ^

3つの一般的な方法があります文字列を作成します。

1. 每当有一个字面值出现的时候,虚拟机就会创建一个字符串
2. 调用String的构造方法创建一个字符串对象
3. 通过+加号进行字符串拼接也会创建新的字符串对象


public class TestString{
    public static void main(String[] args){
        String gareen = "盖伦";
        String temmo = new String("提莫");
        char[] cs = new char[]{'小','明'};
        String hero = new String(cs);
        String hero3 = gareen + temmo;
    }
}

文字列が最後のように変更され、それが継承することはできません。文字列は不変、不変です。

String str = "张三";
str = "李四";
System.out.println(str); //输出李四,str="李四",是重新引用了一个对象,而不是修改了张三这个对象

文字列の一般的な方法。

この方法は、説明
のcharAt(int型のインデックス)が指定された文字位置取得
toCharArray()は文字列対応する取得
SUBSTRING()は、文字列取ら
分割セパレータに応じて分割
末尾のスペースを除去トリム
toLowerCaseメソッドをすべて小文字
のtoUpperCaseをすべて大文字
のindexOf文字列解析をまたは部分が起こるの位置が
含まれている部分文字列が含まれている
すべての置き換えでReplaceAll
のみ最初に取って代わるreplaceFirstというの

、同じオブジェクト==によって判断することができるかどうかを判断するためにどのように、次のコードを参照してください。

public class TestString{
    public static void main(String[] args){
        String str1 = "the light";
        String str2 = new String(str1);
        String str3 = "the light";
        System.out.println(str1 == str2);
        System.out.println(str1 == str3);
    }
}

同じ内容かどうかを確認します。等号のコンテンツを使用して文字列を比較すると、同じケースでなければなりません。コンテンツは一貫しているかどうかを決定する場合は無視equalsIgnoreCase。

public class TestString{
    public static void main(String[] args){
        String str1 = "the light";
        String str2 = new String(str1);
        String str3 = "the light";
        String str4 = "THE LIGHT";
        //判断对象是否一致
        System.out.println(str1 == str2);
        System.out.println(str1 == str3);
        //equals内容一样返回true
        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str4));
        //忽略大小写的比较
        System.out.println(str1.equalsIgnoreCase(str4));
    }
}

文字列が終了するか否かをXX、XXで始まるかどうかを判断するには。

public class TestString1 {

    public static void main(String[] args) {
        String str1 = "the light";

        String start = "the";
        String end = "Ight";

        System.out.println(str1.startsWith(start));//以...开始,得到true
        System.out.println(str1.endsWith(end));//以...结束,得到flase

    }
}

StringBufferの可変長文字列。

この方法は、説明
の追加の追加
、削除、削除
インサート挿入
逆反転
長長
容量容量

コードを見てください。

public class TestString{
    public static void main(String[] args){
        String str1 = "let here";

        StringBuffer sb = new StringBuffer(str1);

        //在最后追加
        sb.append("be light");
        System.out.println(sb);

        //删除4到10之间的字符
        sb.delete(4,10);
        System.out.println(sb);

        //在4插入there
        sb.insert(4,"there ");
        System.out.println(sb);

        //反转
        sb.reverse();
        System.out.println(sb);
    }
}

なぜにStringBufferを長くすることができ、かつ内部は文字配列の文字列と同じで、StringBufferのも、文字の配列を維持しています。しかし、余剰長さを残してこの文字列、。

おすすめ

転載: www.cnblogs.com/yihang996/p/11668867.html