Detect spaces and substrings in some string

  • Thread starter Thread starter juli
  • Start date Start date
J

juli

Good evening!
I have a function that gets a string and suppose to divide it to words
(spaces between each word) and to find if there is specific word
("hello") inside this string.
Do someone know how to do it in c#? Thank you very much!
 
Juli,

Something as this?

\\\
string str = "bla1 bla2 hello bla4";
string[] stra;
stra = str.Split(' ');
for (int i = 0; i<4; i++)
{
if (stra == "hello")
{
MessageBox.Show("The " + (i+1).ToString() +
"th word is hello");
}}
//
 
Little correction,

I am a little bit ashamed of this one

\\\
string str = "bla1 bla2 hello bla4";
string[] stra;
stra = str.Split(' ');
for (int i = 0; i<stra.Lenght; i++)
{
if (stra == "hello")
{
MessageBox.Show("The " + (i+1).ToString() +
"th word is hello");
}}
//
 
Cor said:
Little correction,

I am a little bit ashamed of this one

\\\
string str = "bla1 bla2 hello bla4";
string[] stra;
stra = str.Split(' ');

this doesn't work, you have to pass in an array of char:
stra = str.Split(new char[]{' '});

FB

--
 
Frans Bouma said:
Cor said:
Little correction,

I am a little bit ashamed of this one

\\\
string str = "bla1 bla2 hello bla4";
string[] stra;
stra = str.Split(' ');

this doesn't work, you have to pass in an array of char:
stra = str.Split(new char[]{' '});

Yes it does work, because the Split parameter if declared with the
"params" modifier. Try it :)
 
Jon said:
Frans Bouma said:
Cor said:
Little correction,

I am a little bit ashamed of this one

\\\
string str = "bla1 bla2 hello bla4";
string[] stra;
stra = str.Split(' ');

this doesn't work, you have to pass in an array of char:
stra = str.Split(new char[]{' '});


Yes it does work, because the Split parameter if declared with the
"params" modifier. Try it :)

Aw sh*t, indeed... :) Sorry, Cor, you're right, I was wrong. :)

Notes to self:
bool _doNotPostToUseNet=
((_amountGlassesChampagne>0)||(!_triedInSnippetCompilerFirst));

FB

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top