GeoCap
GeoCap

Reputation: 515

Kotlin substring until specific character

I have a string that starts with 7 characters (let's say XXXXXXX) and after that there are 8 characters that represnet a decimal number. For example: XXXXXXX30.00000 would be 30.00 (I want 2 decimal places) So it should start on index 7 read until dot (.) + 2 decimal places. It should be a string, not a number. I tried with string.substring() but got stuck here.

Upvotes: 1

Views: 2144

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79085

I suggest you do it using the regex, [A-Za-z]{8}(\d+.\d{2})

Explanation of the regex:

  1. [A-Za-z]{8} specifies 8 characters. If you want to support characters other than English alphabets, you can replace [A-Za-z] with \p{L} which specifies unicode letters.
  2. (\d+.\d{2}) specifies a capturing group consisting of digits followed by . which in turn should be followed by 2 digits as per your requirement. A regex pattern can have more than one capturing groups. Since it is the first capturing group in this pattern, it can be accessed by group(1).

A test code:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        // Some test strings
        String[] arr = { "abcdefgh30.00000", "abcdefgh300.0000", "abcdefgh3000.00", "abcdefgh3.00000",
                "abcdefgh0.05000" };
        Pattern pattern = Pattern.compile("[A-Za-z]{8}(\\d+.\\d{2})");
        for (String s : arr) {
            Matcher matcher = pattern.matcher(s);
            if (matcher.find()) {
                String str = matcher.group(1);
                System.out.println(str);
            }
        }
    }
}

Output:

30.00
300.00
3000.00
3.00
0.05

Upvotes: 0

nt95
nt95

Reputation: 595

First you can remove the first 7 characters by

var newString = yourString.removeRange(0,6)

then you can cast to a double if you're certain it will always be a number

var yourNumber = newString.ToDouble()

If you're not sure you can wrap in a try/catch eg:

    try{
        var yourNumber = newString.ToDouble()
    }catch(e:TypeCastException){
       println("Failed to cast to double - "+ e.message)
    }

additionally, to round to a 2 decimal places:

val number2digits:Double = String.format("%.2f", yourNumber).toDouble()

Upvotes: 1

Related Questions