20 maio 2014

Java - String Split with “.” (dot)

package com.lopes.tests;

public class StringSplitWithDot {

    public static void main(String[] args) {

        // Get only 12345
        String number = "12345.00000";

        // Split with error
        String splitError = number.split(".")[0];
        System.out.println("splitNumber " + splitError);

        // Correct way to split by dot(.)
        // "." is a special character. You have to use "\\." to escape this character
        String splitCorrect = number.split("\\.")[0];
        System.out.println("splitNumber " + splitCorrect);

    }

}


Output of Correct way
splitNumber 12345

Sem comentários: