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

Split String - Java

Joined
Jun 30, 2008
Messages
1,145 (0.20/day)
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
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:
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
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
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
27,046 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
you definitely want to use a regular expression for this. to parse the whole thing, dont split
 
Joined
Sep 10, 2008
Messages
532 (0.09/day)
Location
VA/PA
System Name Freyja
Processor Core i7 3770K
Motherboard AsRock Z77 Extreme4
Cooling Cooler Master Hyper 212 EVO
Memory 16 GB GSkill Sniper
Video Card(s) Diamond Radeon HD 7970
Storage Kingston HyperX 240 GB SSD + Seagate 2 TB HD
Display(s) Dell U2410
Case NZXT Tempest 210
Audio Device(s) Asus Xonar Essence STX
Power Supply Seasonic X-Series 750W
Software Ubuntu 13.04 64 bit
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

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):

 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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):

http://img.techpowerup.org/121120/javatest.png

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.
 
Joined
Jun 30, 2008
Messages
1,145 (0.20/day)
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
He should put them in a float array if he will need to do calculation with them.

The answer to my next question :rolleyes:

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

Thanks Kreij
 
Top