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

Split words in a sentence

Hi,

Can someone tell me how to split a words in a sentence at spaces

thanks
kamala
May 23 '07 #1
10 4663
Plater
7,872 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. string mysentance="This is an example sentance";
  2. string[] words=mysentance.Split(" ");
  3. /*
  4. * The results should be something like:
  5. * words[0] = "This"
  6. * words[1] = "is"
  7. * words[2] = "an"
  8. * words[3] = "example"
  9. * words[4] = "sentance."
  10. */
  11.  
May 23 '07 #2
Hi,
Thanks for your reply.
But in Visual C# program,

when i apply this,
it is saying that string is an invalid argument.

someone help me with this

thanks
kamala
May 23 '07 #3
Frinavale
9,735 Expert Mod 8TB
Hi,
Thanks for your reply.
But in Visual C# program,

when i apply this,
it is saying that string is an invalid argument.

someone help me with this

thanks
kamala
Please post a snippet of the code that you're using.
I can't seem to find anything wrong with the code posted by Plater.
May 23 '07 #4
Please post a snippet of the code that you're using.
I can't seem to find anything wrong with the code posted by Plater.
Except his abominable spelling! ;D
May 23 '07 #5
Plater
7,872 Expert 4TB
I am a horrible speller, and English is my first language.

The code I gave was for C#, AND I found my mistake:

Expand|Select|Wrap|Line Numbers
  1. string mysentance = "This is an example sentance";
  2. string[] words = mysentance.Split(" ".ToCharArray());
  3. /*
  4. * The results should be something like:
  5. * words[0] = "This"
  6. * words[1] = "is"
  7. * words[2] = "an"
  8. * words[3] = "example"
  9. * words[4] = "sentance."
  10. */
  11.  
May 24 '07 #6
TRScheel
638 Expert 512MB
I am a horrible speller, and English is my first language.

The code I gave was for C#, AND I found my mistake:

Expand|Select|Wrap|Line Numbers
  1. string mysentance = "This is an example sentance";
  2. string[] words = mysentance.Split(" ".ToCharArray());
  3. /*
  4. * The results should be something like:
  5. * words[0] = "This"
  6. * words[1] = "is"
  7. * words[2] = "an"
  8. * words[3] = "example"
  9. * words[4] = "sentance."
  10. */
  11.  
or...
Expand|Select|Wrap|Line Numbers
  1. string[] words = mysentance.Split(' ');
just use ' instead of " to specify its a char not a string.
May 24 '07 #7
Plater
7,872 Expert 4TB
or...
Expand|Select|Wrap|Line Numbers
  1. string[] words = mysentance.Split(' ');
just use ' instead of " to specify its a char not a string.
It's gotta be a char array so you need

Expand|Select|Wrap|Line Numbers
  1. string[] words = mysentance.Split(new char[]{' '});
  2.  
Which is why I went with the " ".ToCharArray()
May 24 '07 #8
Frinavale
9,735 Expert Mod 8TB
It's gotta be a char array so you need

Expand|Select|Wrap|Line Numbers
  1. string[] words = mysentance.Split(new char[]{' '});
  2.  
Which is why I went with the " ".ToCharArray()
Seems sorta strange.
In VB.NET it's
Expand|Select|Wrap|Line Numbers
  1. Dim words() as String = mysentance.Split(" "c)
  2. 'note that the " "c specifies that it is a character not a String.
  3.  
Why would the Split function need an array of delimiters?
May 24 '07 #9
Split() doesn't require an array, but supports splitting on multiple delimiters. I seem to recall it working fine with just Split(' '), perhaps because the compiler can implicitly cast a single character into an array of size 1, I'm not sure. There are several overloads for the method as well, unless I'm confusing it with Replace().

Seems sorta strange.
In VB.NET it's
Expand|Select|Wrap|Line Numbers
  1. Dim words() as String = mysentance.Split(" "c)
  2. 'note that the " "c specifies that it is a character not a String.
  3.  
Why would the Split function need an array of delimiters?
May 25 '07 #10
Plater
7,872 Expert 4TB
http://msdn2.microsoft.com/en-us/lib...05(VS.71).aspx

The local MSDN help is much more clear on the matter, but it requires an array of params.

From mscorlib.dll
Expand|Select|Wrap|Line Numbers
  1.  //
  2.         // Summary:
  3.         //     Returns a System.String array containing the substrings in this instance
  4.         //     that are delimited by elements of a specified System.Char array.
  5.         //
  6.         // Parameters:
  7.         //   separator:
  8.         //     An array of Unicode characters that delimit the substrings in this instance,
  9.         //     an empty array containing no delimiters, or null.
  10.         //
  11.         // Returns:
  12.         //     An array whose elements contain the substrings in this instance that are
  13.         //     delimited by one or more characters in separator. For more information, see
  14.         //     the Remarks section.
  15.         public string[] Split(params char[] separator);
  16.         //
  17.         // Summary:
  18.         //     Returns a System.String array containing the substrings in this instance
  19.         //     that are delimited by elements of a specified System.Char array. A parameter
  20.         //     specifies the maximum number of substrings to return.
  21.         //
  22.         // Parameters:
  23.         //   count:
  24.         //     The maximum number of substrings to return.
  25.         //
  26.         //   separator:
  27.         //     An array of Unicode characters that delimit the substrings in this instance,
  28.         //     an empty array containing no delimiters, or null.
  29.         //
  30.         // Returns:
  31.         //     An array whose elements contain the substrings in this instance that are
  32.         //     delimited by one or more characters in separator. For more information, see
  33.         //     the Remarks section.
  34.         //
  35.         // Exceptions:
  36.         //   System.ArgumentOutOfRangeException:
  37.         //     count is negative.
  38.         public string[] Split(char[] separator, int count);
  39.         //
  40.         // Summary:
  41.         //     Returns a System.String array containing the substrings in this string that
  42.         //     are delimited by elements of a specified System.Char array. A parameter specifies
  43.         //     whether to return empty array elements.
  44.         //
  45.         // Parameters:
  46.         //   options:
  47.         //     Specify System.StringSplitOptions.RemoveEmptyEntries to omit empty array
  48.         //     elements from the array returned, or System.StringSplitOptions.None to include
  49.         //     empty array elements in the array returned.
  50.         //
  51.         //   separator:
  52.         //     An array of Unicode characters that delimit the substrings in this string,
  53.         //     an empty array containing no delimiters, or null.
  54.         //
  55.         // Returns:
  56.         //     An array whose elements contain the substrings in this string that are delimited
  57.         //     by one or more characters in separator. For more information, see the Remarks
  58.         //     section.
  59.         //
  60.         // Exceptions:
  61.         //   System.ArgumentException:
  62.         //     options is not one of the System.StringSplitOptions values.
  63.         [ComVisible(false)]
  64.         public string[] Split(char[] separator, StringSplitOptions options);
  65.         //
  66.         // Summary:
  67.         //     Returns a System.String array containing the substrings in this string that
  68.         //     are delimited by elements of a specified System.String array. A parameter
  69.         //     specifies whether to return empty array elements.
  70.         //
  71.         // Parameters:
  72.         //   options:
  73.         //     Specify System.StringSplitOptions.RemoveEmptyEntries to omit empty array
  74.         //     elements from the array returned, or System.StringSplitOptions.None to include
  75.         //     empty array elements in the array returned.
  76.         //
  77.         //   separator:
  78.         //     An array of strings that delimit the substrings in this string, an empty
  79.         //     array containing no delimiters, or null.
  80.         //
  81.         // Returns:
  82.         //     An array whose elements contain the substrings in this string that are delimited
  83.         //     by one or more strings in separator. For more information, see the Remarks
  84.         //     section.
  85.         //
  86.         // Exceptions:
  87.         //   System.ArgumentException:
  88.         //     options is not one of the System.StringSplitOptions values.
  89.         [ComVisible(false)]
  90.         public string[] Split(string[] separator, StringSplitOptions options);
  91.         //
  92.         // Summary:
  93.         //     Returns a System.String array containing the substrings in this string that
  94.         //     are delimited by elements of a specified System.Char array. Parameters specify
  95.         //     the maximum number of substrings to return and whether to return empty array
  96.         //     elements.
  97.         //
  98.         // Parameters:
  99.         //   count:
  100.         //     The maximum number of substrings to return.
  101.         //
  102.         //   options:
  103.         //     Specify System.StringSplitOptions.RemoveEmptyEntries to omit empty array
  104.         //     elements from the array returned, or System.StringSplitOptions.None to include
  105.         //     empty array elements in the array returned.
  106.         //
  107.         //   separator:
  108.         //     An array of Unicode characters that delimit the substrings in this string,
  109.         //     an empty array containing no delimiters, or null.
  110.         //
  111.         // Returns:
  112.         //     An array whose elements contain the substrings in this stringthat are delimited
  113.         //     by one or more characters in separator. For more information, see the Remarks
  114.         //     section.
  115.         //
  116.         // Exceptions:
  117.         //   System.ArgumentException:
  118.         //     options is not one of the System.StringSplitOptions values.
  119.         //
  120.         //   System.ArgumentOutOfRangeException:
  121.         //     count is negative.
  122.         [ComVisible(false)]
  123.         public string[] Split(char[] separator, int count, StringSplitOptions options);
  124.         //
  125.         // Summary:
  126.         //     Returns a System.String array containing the substrings in this string that
  127.         //     are delimited by elements of a specified System.String array. Parameters
  128.         //     specify the maximum number of substrings to return and whether to return
  129.         //     empty array elements.
  130.         //
  131.         // Parameters:
  132.         //   count:
  133.         //     The maximum number of substrings to return.
  134.         //
  135.         //   options:
  136.         //     Specify System.StringSplitOptions.RemoveEmptyEntries to omit empty array
  137.         //     elements from the array returned, or System.StringSplitOptions.None to include
  138.         //     empty array elements in the array returned.
  139.         //
  140.         //   separator:
  141.         //     An array of strings that delimit the substrings in this string, an empty
  142.         //     array containing no delimiters, or null.
  143.         //
  144.         // Returns:
  145.         //     An array whose elements contain the substrings in this string that are delimited
  146.         //     by one or more strings in separator. For more information, see the Remarks
  147.         //     section.
  148.         //
  149.         // Exceptions:
  150.         //   System.ArgumentException:
  151.         //     options is not one of the System.StringSplitOptions values.
  152.         //
  153.         //   System.ArgumentOutOfRangeException:
  154.         //     count is negative.
  155.         [ComVisible(false)]
  156.         public string[] Split(string[] separator, int count, StringSplitOptions options);
  157.  
May 25 '07 #11

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

Similar topics

4
by: qwweeeit | last post by:
The standard split() can use only one delimiter. To split a text file into words you need multiple delimiters like blank, punctuation, math signs (+-*/), parenteses and so on. I didn't...
2
by: Robert Oschler | last post by:
Can someone give me a regex expression that will split a sentence containing words and double-quoted phrases, into an array? I don't want the words between the double-quotes to be split using the...
0
by: PLENI SELENE | last post by:
HOW TECHNOLOGY MOVES FROM TALK TO ELECTRONIC DEVICES TO SPEAK GOOD SPANISH! Would you Like To Know How The Electronic Devices Understand When You Talk to Them, And How It Works To...
6
by: gk245 | last post by:
Basically, i want to make a function that will receive a sentence or phrase, and count its words. It would start like this (i think): #include <stdio.h> int count ( char sentence ) {...
2
by: pmwhelan | last post by:
Hi I want to split a string into an array and treat quoted phrases as words. I was splitting on a space which worked fine. char delimiterChars = {' '}; string arrValues =...
2
by: Transcend2030 | last post by:
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. ...
13
by: Chaim Krause | last post by:
I am unable to figure out why the first two statements work as I expect them to and the next two do not. Namely, the first two spit the sentence into its component words, while the latter two...
19
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my...
1
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.