Hi,
Can someone tell me how to split a words in a sentence at spaces
thanks
kamala
10 4663 -
string mysentance="This is an example sentance";
-
string[] words=mysentance.Split(" ");
-
/*
-
* The results should be something like:
-
* words[0] = "This"
-
* words[1] = "is"
-
* words[2] = "an"
-
* words[3] = "example"
-
* words[4] = "sentance."
-
*/
-
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
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.
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
I am a horrible speller, and English is my first language.
The code I gave was for C#, AND I found my mistake: -
string mysentance = "This is an example sentance";
-
string[] words = mysentance.Split(" ".ToCharArray());
-
/*
-
* The results should be something like:
-
* words[0] = "This"
-
* words[1] = "is"
-
* words[2] = "an"
-
* words[3] = "example"
-
* words[4] = "sentance."
-
*/
-
I am a horrible speller, and English is my first language.
The code I gave was for C#, AND I found my mistake: -
string mysentance = "This is an example sentance";
-
string[] words = mysentance.Split(" ".ToCharArray());
-
/*
-
* The results should be something like:
-
* words[0] = "This"
-
* words[1] = "is"
-
* words[2] = "an"
-
* words[3] = "example"
-
* words[4] = "sentance."
-
*/
-
or... -
string[] words = mysentance.Split(' ');
just use ' instead of " to specify its a char not a string.
or... -
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 -
string[] words = mysentance.Split(new char[]{' '});
-
Which is why I went with the " ".ToCharArray()
It's gotta be a char array so you need -
string[] words = mysentance.Split(new char[]{' '});
-
Which is why I went with the " ".ToCharArray()
Seems sorta strange.
In VB.NET it's -
Dim words() as String = mysentance.Split(" "c)
-
'note that the " "c specifies that it is a character not a String.
-
Why would the Split function need an array of delimiters?
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 -
Dim words() as String = mysentance.Split(" "c)
-
'note that the " "c specifies that it is a character not a String.
-
Why would the Split function need an array of delimiters?
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 -
//
-
// Summary:
-
// Returns a System.String array containing the substrings in this instance
-
// that are delimited by elements of a specified System.Char array.
-
//
-
// Parameters:
-
// separator:
-
// An array of Unicode characters that delimit the substrings in this instance,
-
// an empty array containing no delimiters, or null.
-
//
-
// Returns:
-
// An array whose elements contain the substrings in this instance that are
-
// delimited by one or more characters in separator. For more information, see
-
// the Remarks section.
-
public string[] Split(params char[] separator);
-
//
-
// Summary:
-
// Returns a System.String array containing the substrings in this instance
-
// that are delimited by elements of a specified System.Char array. A parameter
-
// specifies the maximum number of substrings to return.
-
//
-
// Parameters:
-
// count:
-
// The maximum number of substrings to return.
-
//
-
// separator:
-
// An array of Unicode characters that delimit the substrings in this instance,
-
// an empty array containing no delimiters, or null.
-
//
-
// Returns:
-
// An array whose elements contain the substrings in this instance that are
-
// delimited by one or more characters in separator. For more information, see
-
// the Remarks section.
-
//
-
// Exceptions:
-
// System.ArgumentOutOfRangeException:
-
// count is negative.
-
public string[] Split(char[] separator, int count);
-
//
-
// Summary:
-
// Returns a System.String array containing the substrings in this string that
-
// are delimited by elements of a specified System.Char array. A parameter specifies
-
// whether to return empty array elements.
-
//
-
// Parameters:
-
// options:
-
// Specify System.StringSplitOptions.RemoveEmptyEntries to omit empty array
-
// elements from the array returned, or System.StringSplitOptions.None to include
-
// empty array elements in the array returned.
-
//
-
// separator:
-
// An array of Unicode characters that delimit the substrings in this string,
-
// an empty array containing no delimiters, or null.
-
//
-
// Returns:
-
// An array whose elements contain the substrings in this string that are delimited
-
// by one or more characters in separator. For more information, see the Remarks
-
// section.
-
//
-
// Exceptions:
-
// System.ArgumentException:
-
// options is not one of the System.StringSplitOptions values.
-
[ComVisible(false)]
-
public string[] Split(char[] separator, StringSplitOptions options);
-
//
-
// Summary:
-
// Returns a System.String array containing the substrings in this string that
-
// are delimited by elements of a specified System.String array. A parameter
-
// specifies whether to return empty array elements.
-
//
-
// Parameters:
-
// options:
-
// Specify System.StringSplitOptions.RemoveEmptyEntries to omit empty array
-
// elements from the array returned, or System.StringSplitOptions.None to include
-
// empty array elements in the array returned.
-
//
-
// separator:
-
// An array of strings that delimit the substrings in this string, an empty
-
// array containing no delimiters, or null.
-
//
-
// Returns:
-
// An array whose elements contain the substrings in this string that are delimited
-
// by one or more strings in separator. For more information, see the Remarks
-
// section.
-
//
-
// Exceptions:
-
// System.ArgumentException:
-
// options is not one of the System.StringSplitOptions values.
-
[ComVisible(false)]
-
public string[] Split(string[] separator, StringSplitOptions options);
-
//
-
// Summary:
-
// Returns a System.String array containing the substrings in this string that
-
// are delimited by elements of a specified System.Char array. Parameters specify
-
// the maximum number of substrings to return and whether to return empty array
-
// elements.
-
//
-
// Parameters:
-
// count:
-
// The maximum number of substrings to return.
-
//
-
// options:
-
// Specify System.StringSplitOptions.RemoveEmptyEntries to omit empty array
-
// elements from the array returned, or System.StringSplitOptions.None to include
-
// empty array elements in the array returned.
-
//
-
// separator:
-
// An array of Unicode characters that delimit the substrings in this string,
-
// an empty array containing no delimiters, or null.
-
//
-
// Returns:
-
// An array whose elements contain the substrings in this stringthat are delimited
-
// by one or more characters in separator. For more information, see the Remarks
-
// section.
-
//
-
// Exceptions:
-
// System.ArgumentException:
-
// options is not one of the System.StringSplitOptions values.
-
//
-
// System.ArgumentOutOfRangeException:
-
// count is negative.
-
[ComVisible(false)]
-
public string[] Split(char[] separator, int count, StringSplitOptions options);
-
//
-
// Summary:
-
// Returns a System.String array containing the substrings in this string that
-
// are delimited by elements of a specified System.String array. Parameters
-
// specify the maximum number of substrings to return and whether to return
-
// empty array elements.
-
//
-
// Parameters:
-
// count:
-
// The maximum number of substrings to return.
-
//
-
// options:
-
// Specify System.StringSplitOptions.RemoveEmptyEntries to omit empty array
-
// elements from the array returned, or System.StringSplitOptions.None to include
-
// empty array elements in the array returned.
-
//
-
// separator:
-
// An array of strings that delimit the substrings in this string, an empty
-
// array containing no delimiters, or null.
-
//
-
// Returns:
-
// An array whose elements contain the substrings in this string that are delimited
-
// by one or more strings in separator. For more information, see the Remarks
-
// section.
-
//
-
// Exceptions:
-
// System.ArgumentException:
-
// options is not one of the System.StringSplitOptions values.
-
//
-
// System.ArgumentOutOfRangeException:
-
// count is negative.
-
[ComVisible(false)]
-
public string[] Split(string[] separator, int count, StringSplitOptions options);
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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 )
{...
|
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 =...
|
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.
...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
| |