473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

split a text

mickey0
142 100+
hello,
I have to read a text and I decided to split it with Scanner class.
Expand|Select|Wrap|Line Numbers
  1.     private Vector<String> splitWords(Scanner s) {
  2.         Vector<String> v = new Vector<String>();        
  3.         while ( s.hasNext() ) {
  4.  
  5.             String token = s.next();
  6.             if ( token.length() == 1 && (token == "." || token == ";" || token == "," || token == ":") ) { 
  7.                 continue;
  8.  
  9.  
  10.             }
  11.             v.add( token );
  12.         }
  13.         return v;                                
  14.     }    
  15.  
and return a vector with all tokens. However as you can see I'd like to don't put in the vector the symbols ; ,.: ? )( etc. I can do as here above (with that ugly if), but I have a text like
"heloworls......................."
how can I delete the ........................... in agodd way? (the scanner doens't separate the "................................"

thanks
Dec 17 '08 #1
5 1509
Nepomuk
3,112 Expert 2GB
Well, have a look at regular expressions. They should do the job. Especially make sure to read this.

Greetings,
Nepomuk
Dec 17 '08 #2
samido
52
string tokenisation my firend, use this class ...!
Dec 18 '08 #3
JosAH
11,448 Expert 8TB
@samido
Tokenizers are the old fashioned way of doing things; Scanners or a simple String.split() handle the job much better (by using regular expressions).

kind regards,

Jos
Dec 18 '08 #4
mickey0
142 100+
@Nepomuk
Hi, I tri to do this to skip every non-word charachter but it dones't work at all.
Expand|Select|Wrap|Line Numbers
  1.         while ( s.hasNext() ) {
  2.             try {
  3.                 s.skip("\\W");
  4.             }
  5.             catch (NoSuchElementException ex) {
  6.  
  7.             }
  8.  
  9.             String token = s.next();
  10.  
What's wrong, please?
Dec 19 '08 #5
JosAH
11,448 Expert 8TB
Try this: Scanner.useDelimiter("\\W+");

kind regards,

Jos
Dec 19 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Blue Ocean | last post by:
In short, it's not working right for me. In long: The program is designed to read numbers from an accumulator and speak them out loud. Unfortunately, the class that contains the method to...
4
by: Brian Glen Palicia | last post by:
My goal is to accept input from the user into a text box and then parse the data using split(). The first step is this tiny program to test the split() function. It runs in IE, but in Mozilla it...
4
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: Sen Haerens | last post by:
I'm using string.split(/^$/m, 2) on a curl output to separate header and body. There’s an empty line between them. ^$ doesn’t seem to work... Example curl output: HTTP/1.1 404 Not Found...
6
by: Saurabh | last post by:
Hi everyone, I am looking for some expert advise to get me out of trouble. I am looking for a solution in C# which will allow me to split the below string in the format provided. The...
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
7
by: AMP | last post by:
Hello, I am trying to split a string at the newline and this doesnt work: String Channel = FileName.Split("\r"); What am I doing wrong? Thanks Mike
5
by: Robert Dodier | last post by:
Hello, I'd like to split a string by commas, but only at the "top level" so to speak. An element can be a comma-less substring, or a quoted string, or a substring which looks like a function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.