B. Money
B. Money

Reputation: 941

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
        }

Upvotes: 1

Views: 802

Answers (1)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

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.

Upvotes: 1

Related Questions