473,656 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with string.split()

1 New Member
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(wor ds);

I've played around with sentence.compar eTo() 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 4759
r035198x
13,262 MVP
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(wor ds);

I've played around with sentence.compar eTo() 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 Recognized Expert MVP
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
1890
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, 7, 8, 9, bar, foo_6, foo_7, foo_8, foo_9, foo_10, foo_11 I need to read user input of a subset of these. The user will type a set of names separated by commas (with optional white space), but there may also be sequences indicated by a dash...
5
7563
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 = "field1;value1"; local($field, $value) = split(/$separator/, $line);
23
2082
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 becomes a different operation than with regular strings because the circular string can be "rotated", like this: 1 2 3 4 5 2 3 4 5 1 3 4 5 1 2 4 5 1 2 3
8
5025
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 see below from excel, word, notepad, etc.. into a textbox on my form. I need to break each line into 2 numbers which I'll use as parameters for another function. in all cases each line will be separated with a vbNewline and in most cases the 2...
3
2069
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 traceback of: Traceback (most recent call last): File
9
2187
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 that has problems. FullName=Request.Form("Name") Email=Request.Form("Email") GivenName=Request.Form("GivenName")
3
3625
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 from object to string frequently). Basically generics and not its non- generic counterpart. string str1 = "abc: value1 def: value2 ghi: value3"; char delimiterChars = { '\t' }; string tokens = str1.Split(delimiterChars);
17
4478
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 and an operation, with and output of the answer with the original equation. I think I need to use the split function, however I dont know what to do once it is split. Here is what I have so far- import javax.swing.*; import java.awt.*; import...
2
1401
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 earliest. The program takes a string and sums all numbers inside it. I'm testing with the following string: "123xx,22! p1" which should yield a sum of 123 + 22 + 1 = 146. My solution to this exercise was to write a function that takes a string and...
0
8296
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,...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
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
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
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...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
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
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
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.