473,805 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

split a text

mickey0
142 New Member
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 1523
Nepomuk
3,112 Recognized Expert Specialist
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 New Member
string tokenisation my firend, use this class ...!
Dec 18 '08 #3
JosAH
11,448 Recognized Expert MVP
@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 New Member
@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 Recognized Expert MVP
Try this: Scanner.useDeli miter("\\W+");

kind regards,

Jos
Dec 19 '08 #6

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

Similar topics

5
27210
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 read off large numbers is only for integers. My intention is to split a String across the Regex of ".". However, this code does not work: private void doRealValueOf(String text) {
4
3610
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 just hangs and keeps loading forever. I checked around on the web and in USENET, but I haven't seen any mention of split() not working in Mozilla. Thoughts? Thanks in advance. <HTML> <HEAD> </HEAD>
4
728
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 ignore pairs of brackets like: "one" <tab> "two three" ( four "five six" ) Want fields like:
4
3858
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, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
3
9671
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... field1...field2...field3...
5
8714
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 Date: Wed, 22 Feb 2006 00:01:45 GMT Server: Apache/1.3.33 (Darwin) PHP/5.1.2 mod_perl/1.29 Transfer-Encoding: chunked Content-Type: text/html; charset=iso-8859-1
6
2173
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 String.Split() allows only a single char as a delimiter.So I cannot use that as well. Original String "*** MASTER (Supervisor) *** September 23, 1997 at 3:22pm \r\nThis is the first line of text. There will be many more beneath this on so look...
24
4867
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 multiple lines. It would appear though that every distinct set of data starts with a 'code' that is always the 25 characters long. The text is variable however. Assuming i've read the contents of the file into the string myfile, how do i split my...
7
9312
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
4781
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 call. If some element contains commas, I don't want to split it. Examples: 'foo, bar, baz' ='foo' 'bar' 'baz'
0
9718
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.