|
Q4009 How do I parse a comma delimited string?
irt.org | Knowledge Base | Java | Q4009 [ previous next ]
Q4009 How do I parse a comma delimited string?
Use the StringTokenizer object, the following is a snippet of code which fills in
4 values (Open, High, Low and Close) in respective variables from an string ohlc which
is passed as a parameter to the method
public Candle(String ohlc){
/**
* The StringTokenizer class lets us separate ohlc
* using custom separators. I used a space, a colon, a comma,
* and a semicolon as separators.
*/
StringTokenizer st=new StringTokenizer(ohlc," :,;");
open=(new Double(st.nextToken())).doubleValue();
high=(new Double(st.nextToken())).doubleValue();
low=(new Double(st.nextToken())).doubleValue();
close=(new Double(st.nextToken())).doubleValue();
}
|
|
|
Copyright © 1996-2009 irt.org, All Rights Reserved.