473,322 Members | 1,540 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,322 software developers and data experts.

Need help with string.split()

Hi, I'm having problems with string.split()

My problem is;

I have a sentence which the user inputs, I then input a word and the number of times that word appears in the sentence is displayed.

Is there a regex expression which will exclude every word except "word" which I entered. So that words.length will actually display the number of "word" in the sentence.

Here is some of my code; The ??!word?? is where I'm stuck. Is this a valid approach? Even if I skip the sentence2.split(word) I get only 1 word being counted, which isnt right when theres more than 1 word in the sentence.

String sentence1 = sentence.trim();
String sentence2 = sentence1.split("\\s+");

word = GetWord();

String words[] = sentence2.split(??!word??);

DisplayWord(words);

I've played around with sentence.compareTo() as well, to iterate through the sentence comparing each string to word, but it hasnt worked either. My output was always no word present.

My DisplayWord code is just an if-else statement (if word.length > 0).

Thanks for any help anyone can provide. Much appreciated.

Brett
May 16 '07 #1
2 4742
r035198x
13,262 8TB
Hi, I'm having problems with string.split()

My problem is;

I have a sentence which the user inputs, I then input a word and the number of times that word appears in the sentence is displayed.

Is there a regex expression which will exclude every word except "word" which I entered. So that words.length will actually display the number of "word" in the sentence.

Here is some of my code; The ??!word?? is where I'm stuck. Is this a valid approach? Even if I skip the sentence2.split(word) I get only 1 word being counted, which isnt right when theres more than 1 word in the sentence.

String sentence1 = sentence.trim();
String sentence2 = sentence1.split("\\s+");

word = GetWord();

String words[] = sentence2.split(??!word??);

DisplayWord(words);

I've played around with sentence.compareTo() as well, to iterate through the sentence comparing each string to word, but it hasnt worked either. My output was always no word present.

My DisplayWord code is just an if-else statement (if word.length > 0).

Thanks for any help anyone can provide. Much appreciated.

Brett
Run through this and you should be able to get it right. You can always post again of course if you still have problems with it.
May 16 '07 #2
JosAH
11,448 Expert 8TB
Have a look at the String.indexOf("word", startPos) method and use it
in a loop where you count the successful matches.

kind regards,

Jos
May 16 '07 #3

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

Similar topics

10
by: george young | last post by:
For each run of my app, I have a known set of (<100) wafer names. Names are sometimes simply integers, sometimes a short string, and sometimes a short string followed by an integer, e.g.: 5, 6,...
5
by: Arjen | last post by:
Hi All, What I want to is using a string as PATTERN in a split function. This makes it possible for me to change the PATTERN on one place in my script... For example: $separator = ";"; $line...
23
by: Rogers | last post by:
I want to compare strings of numbers that have a circular boundary condition. This means that the string is arranged in a loop without an end-of-string. The comparaison of two strings now...
8
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and I need to parse strings that look similar to the one below. All 5 rows will make up one string. I have a form where a use can copy/paste data like what you...
3
by: jerry.levan | last post by:
Hi, I have a Tktable object (self.table) and when I click on a row the whole row is selected. If I click of a button to get the row contents then self.table.curselection() fails with a...
9
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but...
3
by: Dave | last post by:
I'm calling string.Split() producing output string. I need direct access to its enumerator, but would greatly prefer an enumerator strings and not object types (as my parsing is unsafe casting...
17
by: Max347 | last post by:
This is my first post, so hopefully I can give enough information. I am running windows xp, using jGrasp to write code. I need to make a calculator in which the user inputs 2 floating point numbers...
2
by: WP | last post by:
Hello, below is my very first python program. I have some questions regarding it and would like comments in general. I won't be able to get my hands on a good python book until tomorrow at the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.