# Split String - Java



## Wozzer (Nov 20, 2012)

Hi all,

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


```
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


----------



## Kreij (Nov 20, 2012)

You could try using the regex *or* operator.

```
String[] temp = MetaData.latitude.split("°|\\'|\\"")
```

I'm not sure if I got the regex syntax correct. lol


----------



## W1zzard (Nov 20, 2012)

you definitely want to use a regular expression for this. to parse the whole thing, dont split


----------



## Maelstrom (Nov 20, 2012)

Kreij said:


> You could try using the regex *or* operator.
> 
> ```
> String[] temp = MetaData.latitude.split("°|\\'|\\"")
> ...



What you wrote throws an error when compiling. Should be:

```
String[] temp = MetaData.latitude.split("°|\'|\"")
```

The regex stuff does work though (Thanks for showing that Kreij! Didn't know about that):


----------



## Kreij (Nov 20, 2012)

Maelstrom said:


> What you wrote throws an error when compiling. Should be:
> 
> ```
> String[] temp = MetaData.latitude.split("°|\'|\"")
> ...



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.


----------



## Wozzer (Nov 20, 2012)

Kreij said:


> He should put them in a float array if he will need to do calculation with them.



The answer to my next question 

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

Thanks Kreij


----------

