简体   繁体   中英

How do I split a string by character position?

I want to split a two strings of numbers 12345678 and -12345678, so that i can insert a decimal point after the first two integers in each instance. This is what i'm working with so far..

String googlelat = String.valueOf((int) (location.getLatitude()*1E6)); //<-- equals 12345678
        if (googlelat.length() <= 8  ){
                        //split after second integer
                        //insert decimal 
            //make string 12.345678
        }
        String googlelon = String.valueOf((int) (location.getLongitude()*1E6)); //<-- equals -12345678
                    if (googlelon.length() >  8  ){
                        //split after third character
                        //insert decimal 
            //make string -12.345678
        }

Try it this way....

1. Take the String value into StringBuilder .

   StringBuilder sb = new StringBuilder(googlelat);

2. Use insert() method of StringBuilder.

   sb.insert(2,".");

And its DONE !!! Try the same way for Longitude.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM