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

Beginners question

Hello, I'm new to C# and I am having a small problem. All I want to do is
take a sentence from the user then replace certain words with other ones.
So I started off with this:

string myString = Console.ReadLine();
string editString;
editString = myString.Replace("is", "isn't");
Console.WriteLine(editString);

Which if I entered "This is a test" I got back "Thisn't isn't a test"
I got round this by looking for the word to replace by putting spaces round
the string:

string myString = Console.ReadLine();
string editString;
editString = myString.Replace(" is ", " isn't ");
Console.WriteLine(editString);

Which was fine for this example, but what if there is punctuation involved?
what if I wanted to convert "This is." it wouldn't pick it up because of the
full stop. Would it be best to do it a different way by stripping all the
punctuation out somehow? but then how would you put it back in? is there a
simple way of doing this?
Thanks for any help,
Andy
Nov 15 '05 #1
5 1488
Take a look at Regex.Replace and regular expressions.
HTH
Alex

"Andy Hoe" <an*********@dsl.pipexBLAH.comBLAH> wrote in message
news:3f**********************@news.dial.pipex.com. ..
Hello, I'm new to C# and I am having a small problem. All I want to do is
take a sentence from the user then replace certain words with other ones.
So I started off with this:

string myString = Console.ReadLine();
string editString;
editString = myString.Replace("is", "isn't");
Console.WriteLine(editString);

Which if I entered "This is a test" I got back "Thisn't isn't a test"
I got round this by looking for the word to replace by putting spaces round the string:

string myString = Console.ReadLine();
string editString;
editString = myString.Replace(" is ", " isn't ");
Console.WriteLine(editString);

Which was fine for this example, but what if there is punctuation involved? what if I wanted to convert "This is." it wouldn't pick it up because of the full stop. Would it be best to do it a different way by stripping all the
punctuation out somehow? but then how would you put it back in? is there a
simple way of doing this?
Thanks for any help,
Andy

Nov 15 '05 #2
100
Hi Andy,

Take a look on *Regex* class and regular expressions

HTH
B\rgds
100

"Andy Hoe" <an*********@dsl.pipexBLAH.comBLAH> wrote in message
news:3f**********************@news.dial.pipex.com. ..
Hello, I'm new to C# and I am having a small problem. All I want to do is
take a sentence from the user then replace certain words with other ones.
So I started off with this:

string myString = Console.ReadLine();
string editString;
editString = myString.Replace("is", "isn't");
Console.WriteLine(editString);

Which if I entered "This is a test" I got back "Thisn't isn't a test"
I got round this by looking for the word to replace by putting spaces round the string:

string myString = Console.ReadLine();
string editString;
editString = myString.Replace(" is ", " isn't ");
Console.WriteLine(editString);

Which was fine for this example, but what if there is punctuation involved? what if I wanted to convert "This is." it wouldn't pick it up because of the full stop. Would it be best to do it a different way by stripping all the
punctuation out somehow? but then how would you put it back in? is there a
simple way of doing this?
Thanks for any help,
Andy

Nov 15 '05 #3
Hi,

Regular expressions have a zero-width assertation \b. This specifies that
the match must occur on a word boundary.

using System.Text.RegularExpressions;
string myString = Console.ReadLine();
string editString; editString = Regex.Replace (myString,"\\bis","isn't"); Console.WriteLine(editString);

HTH
greetings
"Andy Hoe" <an*********@dsl.pipexBLAH.comBLAH> wrote in message
news:3f**********************@news.dial.pipex.com. .. Hello, I'm new to C# and I am having a small problem. All I want to do is
take a sentence from the user then replace certain words with other ones.
So I started off with this:

string myString = Console.ReadLine();
string editString;
editString = myString.Replace("is", "isn't");
Console.WriteLine(editString);

Which if I entered "This is a test" I got back "Thisn't isn't a test"
I got round this by looking for the word to replace by putting spaces round the string:

string myString = Console.ReadLine();
string editString;
editString = myString.Replace(" is ", " isn't ");
Console.WriteLine(editString);

Which was fine for this example, but what if there is punctuation involved? what if I wanted to convert "This is." it wouldn't pick it up because of the full stop. Would it be best to do it a different way by stripping all the
punctuation out somehow? but then how would you put it back in? is there a
simple way of doing this?
Thanks for any help,
Andy

Nov 15 '05 #4
Why don't you say
editString = myString.Replace (" is", " isn't");

"Andy Hoe" <an*********@dsl.pipexBLAH.comBLAH> wrote in message news:<3f**********************@news.dial.pipex.com >...
Hello, I'm new to C# and I am having a small problem. All I want to do is
take a sentence from the user then replace certain words with other ones.
So I started off with this:

string myString = Console.ReadLine();
string editString;
editString = myString.Replace("is", "isn't");
Console.WriteLine(editString);

Which if I entered "This is a test" I got back "Thisn't isn't a test"
I got round this by looking for the word to replace by putting spaces round
the string:

string myString = Console.ReadLine();
string editString;
editString = myString.Replace(" is ", " isn't ");
Console.WriteLine(editString);

Which was fine for this example, but what if there is punctuation involved?
what if I wanted to convert "This is." it wouldn't pick it up because of the
full stop. Would it be best to do it a different way by stripping all the
punctuation out somehow? but then how would you put it back in? is there a
simple way of doing this?
Thanks for any help,
Andy

Nov 15 '05 #5
Carol Sun <ap****@hotmail.com> wrote:
Why don't you say
editString = myString.Replace (" is", " isn't");


That has the same problem with other words:

"This is an issue that needs resolving" would become
"This isn't an isn'tsue that needs resolving"

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Will | last post by:
Pardon two post in a row to the newsgroup but I want to try and expedite this, if you guys don't mind helping out... I running Windows XP Pro and wanted to download Python and any additional...
4
by: blah | last post by:
I m actually a Novice in Python as well as Linux, When i look up things on the internet about Linux Flavours, They are written so complex that it is difficult for me to understand, i am asking if...
4
by: Eggnog | last post by:
Hi, Is there a newsgroups for beginners questions? Cheers, Nawg
6
by: William Foster | last post by:
Does anyone know of a good online tutorial for C# focused on beginners. I have been to many great sites like csharpfriends, csharp-corner etc looking for good tutorials and have had no luck. Any...
18
by: John Salerno | last post by:
Hi all. I was hoping some of you could recommend a book or two that would help me get started with the basics of C#. I have a slight knowledge of programming, but basically I want to start out like...
2
by: =?Utf-8?B?UmFrZXNo?= | last post by:
Hi, I have worked in Windows development for some time and want to learn ASP.Net - Web Application. Can some one redirect me to some site where I can find some good start up for beginners course...
4
by: aman firoz | last post by:
do you people got anything for c beginners..... help me out guys
0
by: Dual_b00t | last post by:
hi i created a site called PHP Together its for beginners also for gurus to help out the beginners if you are learning PHP and feel alone then drop by . ciao Mark
19
by: yltkhuu | last post by:
1. How does having a widely adopted C++ standard help game programmers? 2. What are the advantages ans disadvantages of employing the "using" directive? 3. Why might you define a new name for an...
6
by: Lars | last post by:
Hi all If this is the wrong list for beginners trouble I apologize. please refer to me the correct list in such case. I am trying to create a simple linked list but I keep getting segmentation...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.