1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Split String - Java

Discussion in 'Programming & Webmastering' started by Wozzer, Nov 20, 2012.

  1. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,133 (0.51/day)
    Thanks Received:
    68
    Location:
    England
    Hi all,

    I've created a program to split a string which contains latitude coordinates.

    Code:
     String[] temp;
            
            String delimiter = "°";
            
            temp = MetaData.latitude.split(delimiter);
     
            for (int i = 0; i < temp.length; i++) {
                System.out.println(temp[i]);
            }
    
    For arguments sake, here is a latitude string: 52°97'0.6822"

    The program above will only do up to 97. I was thinking of using a loop, but unsure how to go about doing it.

    Any input would be helpful.

    Cheers

    :toast:
  2. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (5.11/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    You could try using the regex or operator.
    Code:
    String[] temp = MetaData.latitude.split("°|\\'|\\"")
    
    I'm not sure if I got the regex syntax correct. lol
    Wozzer and Maelstrom say thanks.
  3. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    14,632 (3.94/day)
    Thanks Received:
    11,363
    you definitely want to use a regular expression for this. to parse the whole thing, dont split
  4. Maelstrom

    Joined:
    Sep 10, 2008
    Messages:
    534 (0.25/day)
    Thanks Received:
    159
    Location:
    VA/PA
    What you wrote throws an error when compiling. Should be:
    Code:
    String[] temp = MetaData.latitude.split("°|\'|\"")
    
    The regex stuff does work though (Thanks for showing that Kreij! Didn't know about that):

    [​IMG]
    Kreij says thanks.
  5. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (5.11/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    Yeah, that why I said I wasn't sure about the syntax and if it needed to be double backslashed.
    I'm also not sure what he plans on doing with the coordinates once they are split out.
    He should put them in a float array if he will need to do calculation with them.
  6. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,133 (0.51/day)
    Thanks Received:
    68
    Location:
    England
    The answer to my next question :rolleyes:

    I'm converting decimal degrees into degrees, minutes, seconds.

    Thanks Kreij

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page