• Post Reply Bookmark Topic Watch Topic
  • New Topic

Breaking a string down and tokenizers - help!  RSS feed

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi People.

I'm trying to read in a line of text from a text file.



I want to seperate the string so that



I have used tokenizers to break strings down in the past but I don't know how to make it so that I can set the ',' character as the delimiter in order to seperate these out into tokens.

Anyone know how to do this? Do I need to use something else other than a stringTokenizer, as I only know how to extract words from a string that uses a space as the delimiter?

Cheers
[ December 18, 2005: Message edited by: Sam Bluesman ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand your question. StringTokenizer has a constructor that accepts a delimiter, or the no-argument constructor will use a blank space as delimiter.

A preferred approach now would be to use the split method in String.
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you could use.

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ December 18, 2005: Message edited by: Tony Morris ]
 
xu kuang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programming, Dr David Wake, Monday, 0900, 150

I write a function as follow:

function strToArray(str,ch){
var a=new Array();
var i=0,j=-1,k=0;
while (i<str.length){
j=str.indexOf(ch,j+1);
if (j!=-1){
if (j==0){a[k]="";}else{a[k]=str.substring(i,j);}
i=j+1;
} else {
a[k]=str.substring(i,str.length);
i=str.length;
}
k++;
}
return a;
}

"str" is your string which need to split;"ch" is a sign,for example ",";
you may get it strToArray(str,,);
so return result is a array:

Programming
Dr David Wake
Monday
0900
150

I don't konw it can give you some help??
My english is very bad,sorry.
other ,I am from China.

msn:xoy2129@hotmail.com
welcome!!
欢迎交流!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by xu kuang:

I write a function as follow:

function strToArray(str,ch){
...


Your code is in JavaScript; this is a completely different language from Java, the language that is the subject of this forum. We do have one forum here at the ranch that talks about JavaScript here, but elsewhere on the Ranch, JavaScript is off-topic.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!