how to find a word in c#
hi
I'm new to C# and I didn't have any programming skills.
I'm on my third week now and it seems it's quite complex
I would like to design a code, where it can determine the word type in the sentence.
So, first one must type a sentence and then on the second box, just type a word and check if its present in the first box (from the sentence you typed)
the result should show, true or false.
I started a code but I don't know how to continue: Any idea or suggestion is greatly appreciated:
namespace DisplayWordJaNein
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string yourSentence;
string yourWord;
yourSentence = textBox1.Text;
yourWord = textBox2.Text;
if
from here I'm not sure what to do. From what I've learned I have to use the type bool here. But don't know how to implement.
thanks & regards
tintincute
Junior Poster in Training
55 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
hi again
i was able to finish it until here and it's working. But what I like is, it will display "Word Found" or let's say true if I'll write a word which is on the sentence itself.
ok, I tried to write: this is my first sentence
and on the 2nd box, if i write "sentence" only it displays "the word can't be found".
so i think i'm on the right track but i'd like that it will give me result "true" if the word i type is on the sentence
thanks
here is my code:
namespace DisplayWordJaNein
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string yourSentence;
string yourWord;
yourSentence = textBox1.Text;
yourWord = textBox2.Text;
if (string.Equals(yourSentence,yourWord))
{
label4.Text="Word Found!";
}
else
{
label4.Text="The word could not be found.";
}
tintincute
Junior Poster in Training
55 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
Well, google will perhaps help you in finding things in strings. There are a number of options you could use..
LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
Look up the members of the String class.
From the code I see you use some Visual Studio.
In the menubar you will find something called "Help"
Click on it and choose Search. In the window that appears type String members
You will find what you where looking for.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
Take a look at string.Contains() or string.Compare() instead of string.Equals(). You will also need to address the casing of the word (uppercase, lowercase).
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
Hi
I was able to find it. But my next problem is: I would like it that it will also accept the UPPERCASE or LOWERCASE letters if I type in the "enter word to compare".
So that means if I write in the first box like:
This is my sentence
And I'll write in my second box like:
SENTENCE,
that it wil also determine that way.
Is that posible?
Coz in my code, I wrote the big letters SENTENCE but then it didn't find the word.
Shall I make another method for that?
Thanks again
namespace DisplayWordJaNein
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string yourSentence;
string yourWord;
yourSentence = textBox1.Text;
yourWord = textBox2.Text;
if (yourSentence.Contains(yourWord))
{
label4.Text = "Word Found! / ";
}
else
{
label4.Text="The word could not be found.";
}
}
tintincute
Junior Poster in Training
55 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
then convert the strings to the same casing and test after.
LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
hmm where shall I type this command,
convert strings?
is it under the if method?
thanks
tintincute
Junior Poster in Training
55 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
Hi again,
ok I tried this one, but then it displays the word if I write the word. But the bug is if I only write a letter which a word has, it also displays "Word Found"
I would like that it will only determine the word not the characters.
In this program:
if I type on the first box:
This is my sentence
and type on the 2nd box:
sentence
it will give a result of word found
but then if i type in the first box:
s
it will give a result of word found.
any suggestions?
thanks
if (yourSentence.Contains(yourWord.ToLower()))
{
label4.Text = "Word Found! / ";
}
else
{
label4.Text="The word could not be found.";
}
tintincute
Junior Poster in Training
55 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
yourWord = "s"
and yourSentence = "This is my sentence"
yourWord will 2 times be found in yourSentence!
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
why is that?
I just would like to display the word which I exactly type, not the letters.
is my code not correct?
thanks
tintincute
Junior Poster in Training
55 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
Your code is correct.
What you see as a letter "s" is actually "seen" by the computer as a string. Even "" is a string!
The Contains method just checks if a string is contained in another string or not.
Possible remedy(by no means the best!) to your problem : Only continue if yourWord.Length is greather then 1.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
hi ddanbe, thanks for the reply.
I think I got and it's working fine. Now this code will also display the word which is written for example in Uppercase or Lowercase.
this is what i did:
thanks guys. I'm starting to enjoy this stuff...
do you know a website where it gives "to do's" or "assignment" for C#. you know for beginners like me. I would like to do some practice. Thanks again
private void button1_Click(object sender, EventArgs e)
{
string yourSentence;
string yourWord;
yourSentence = textBox1.Text.ToLower();
yourWord = textBox2.Text.ToLower();
if (yourSentence.Contains(yourWord))
{
label3.Text = "Word Found!";
}
else
{
label3.Text = "The word can't be found!";
}
}
tintincute
Junior Poster in Training
55 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
best answer get a book and follow through the things it asks you to make
LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190