943,589 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 11753
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 25th, 2009
0

how to find a word in c#

Expand Post »
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
Last edited by tintincute; Feb 25th, 2009 at 7:56 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
tintincute is offline Offline
55 posts
since Feb 2009
Feb 25th, 2009
0

Re: how to find a word in c#

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.";
}
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
tintincute is offline Offline
55 posts
since Feb 2009
Feb 25th, 2009
0

Re: how to find a word in c#

Well, google will perhaps help you in finding things in strings. There are a number of options you could use..
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 25th, 2009
0

Re: how to find a word in c#

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.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Feb 25th, 2009
0

Re: how to find a word in c#

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).
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Feb 25th, 2009
0

Re: how to find a word in c#

C# Syntax (Toggle Plain Text)
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. string sText="Sonia Sardana";
  4. if (sText.Contains("Sonia"))
  5. {
  6. MessageBox.Show ("contains");
  7. }
  8. }
Reputation Points: 0
Solved Threads: 8
Posting Whiz
sonia sardana is offline Offline
326 posts
since Mar 2008
Mar 2nd, 2009
0

Re: how to find a word in c#

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.";
}
}
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
tintincute is offline Offline
55 posts
since Feb 2009
Mar 2nd, 2009
0

Re: how to find a word in c#

then convert the strings to the same casing and test after.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Mar 2nd, 2009
0

Re: how to find a word in c#

hmm where shall I type this command,
convert strings?
is it under the if method?
thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
tintincute is offline Offline
55 posts
since Feb 2009
Mar 2nd, 2009
0

Re: how to find a word in c#

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.";
}
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
tintincute is offline Offline
55 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: just a little help
Next Thread in C# Forum Timeline: Media Converter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC