0
0

【Java】指定した開始位置から、指定したバイト数をStringで返す

Posted at

■ 関数定義部分

public static String Bytes_CUT(String str, int start, int get_length) {

引数

1:str 
byte形式に変換して切り取る文字列全体。

2:start
str の何バイト目から切り取りたいかの指定

3:get_length
start から始めて、何バイトの値を取得したいか

コード


/**
*   cut_start から、get_length 分の値を取得する
*/
    public static String Bytes_CUT(String str, int start, int get_length) {

        // string => byte へ変換
        byte[] get_def = str.getBytes(StandardCharsets.UTF_8);

        int cut_start = start - 1; // index 考慮用に -1 をした値
        int cut_end = (start + get_length) - 1;    // cut_start から、get_length 分の値を取得する


        byte[] result_def = new byte[get_length];
        int idx = 0;
        for (int i = 1; i <= 190; i++) {
            if (i >= cut_start && i < cut_end) {
                result_def[idx] = get_def[i];
                idx = idx + 1;
            }
        }

        // String で上で切り取った値を返す
        String return_str = new String(result_def, StandardCharsets.UTF_8);
        return return_str;

    }

Main 部分で関数使用

String get_QR = scanResult.getContents();
String Get_CODE = Bytes_CUT(get_QR,30,11);
System.out.println("取得コード:::" + Get_CODE);

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0