how can i break apart this string?

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
I need to break this up into 3 strings

"Cheney" "Marshall" 7


String1 = "Cheney";
String2 = "Marshall";
String3 = "7";


keeping in mind that sometimes one of the town names may consist of two words

such as "Coeur d'Alene"

I've come up with semi-solutions but im guessing there is a more elegant way to handle it
 

Cogman

Lifer
Sep 19, 2000
10,286
145
106
Originally posted by: VinylxScratches
Where are you grabbing the data from?

This is the all important question. Where you get the text from will completely determine how complex your string processing system should be.

For simple cases, just look for spaces. After that, it really depends on what you want to look for. If your string comes in as "CheneyMarshall7" Then you might have a rough time, One trick would be to use a dictionary of valid words and pull them out of there, another would be to require capital letters for new words and split it that way.
 

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
it comes from a file

with a set of towns listed in this format

"Cheney" "Marshall" 7
"Cheney" "Coeur d'Alene" 35
etc

I think i got it working, although using string.split fills in slots 1, 3 and 4 of an array with the rest being empty strings
 

presidentender

Golden Member
Jan 23, 2008
1,166
0
76
String.split works. You'll probably want to trim the string containing the int since it will have a leading space otherwise.
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
import java.util.*;
import java.io.*;
import java.lang.*;

public class delmin
{
public static void main(String[] args)
{
try {
ArrayList words = new ArrayList();
Scanner file = new Scanner(new FileReader("file.txt")).useDelimiter("[\"\\n]");
while (file.hasNext()){
if (!file.hasNext("\\s*"))
words.add(file.next().trim());
else
file.next();
}
for (int i = 0; i < words.size(); i++)
System.out.println(words.get(i));

}catch (Exception e){
throw new IllegalArgumentException ("Invalid File Name");
}
}
}
 

alocurto

Platinum Member
Nov 4, 1999
2,174
0
76
Looks to me like you can use a split on "

That is '" ' (The space is included)

That will leave you with:

"Cheney
"Marshall
7

But then you have to clean it up...

Maybe get the 2 index locations of '" ' and then sub string it.

fname = str.substring(1, locationOfFirstDelim)
lname = str.substring(locationOfFirstDelim, locationOfSecondDelim)
num = str.substring(locationOfLastDelim, str.length - locationOfSecondDelim)

You would have to cast the num variable, check those int locations to make sure that you don't have bad numbers making you look for data outside of the string.

Just another way to do it. Don't forget "Steve Jay Smith" could probably be a name and the solution you mentioned will break.
 

flashbacck

Golden Member
Aug 3, 2001
1,921
0
76
You don't want the quotes in your string, right? I think StringTokenizer would be easier in that case. You can specify the double quote character and a space character as delimiters in the constructor. StringTokenizer is also slightly faster that String.split, though you might not observe the difference unless you're doing thousands of splits.

edit: my mistake, I just noticed you might have spaces in the words between quotes. You could still use StringTokenizer, you'd just have to remember to remove the space before the last number.
 

The J

Senior member
Aug 30, 2004
755
0
76
I just checked and tfinch2 is correct: StringTokenizer seems to be deprecated. The Java 5 docs say to use the String.split() method instead. Searching Google doesn't return any Java 6 docs for StringTokenizer, so I guess that means that the class is not in Java 6.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
I'm used to building J2EE apps where we still only have access to 1.4 classes. Wasn't even aware that StringTokenizer was deprecated in later versions.