|
-
May 1st, 2012, 10:08 PM
#1
Text Replacing code
Hello,
I'm trying to figure out how to replace characters with a word.
Like this d would be delta, D would be Delta.
I have been trying strings, streambuilder, with no luck.
Code:
private void button1_Click(object sender, EventArgs e)
{
string strValue = "dD";
string strResult0 = strValue.Replace("D", "Delta", "d", "delta");
MessageBox.Show(strResult0);
Any suggestions would be great.
Thanks,
Mike
-
May 2nd, 2012, 06:23 AM
#2
Re: Text Replacing code
As long as there's no intersection (like in this case), you can simply call the Replace method twice.
Code:
string strResult0 = strValue.Replace("D", "Delta");
strResult0 = strValue.Replace("d", "delta");
//print strResult0...
-
May 2nd, 2012, 07:39 AM
#3
Re: Text Replacing code
Hello, Talikag
So if I have a A-Z,a-z replacing all letters with words, will this not work.
Because basically what I'm needing is to read a textbox, maybe to string and then replace all letters with either upper case or lower case word.
If textbox1 had AaBbCc, it would convert it to this new string:
ALFA alfa BRAVO bravo CHARLIE charlie
string strResult0 = strValue.Replace("A", "ALFA");
strResult0 = strValue.Replace("a", "alfa");
strResult0 = strValue.Replace("B", "BRAVO");
strResult0 = strValue.Replace("b", "bravo");
strResult0 = strValue.Replace("C", "CHARLIE");
strResult0 = strValue.Replace("c", "charlie");
I'm new to using strings and streambuilders, so I have been trying to figure out how to make this work, first by replacing the words, then add the textbox read fuction to it,
The user would enter only letters into a textbox then I will convert them to phonetic words, displayed in another textbox.
Any help would be great.
Thanks,
Mike
-
May 2nd, 2012, 02:26 PM
#4
Re: Text Replacing code
Replace(searchString, replaceString) will replace any occurrence of the "search string (or char)", so the problem here is, if any of the replacement words contains such a substring (or char), it too will be replaced the next time you call the Replace() method.
This is what Talikag referred to as "intersections".
So, if s = "dD", then:
string s1 = s.Replace("D", "didactics"); // s1 = "ddidactics" -- note two d's at the start
string s2 = s1.Replace("d", "boring"); // s2 = "boringboringiboringactics" -- every d is replaced
What you could do, for example, in case there are intersections, is to use the template string to get a char array from it, and then loop through it, examining each char and deciding what string to append to your result string (or, better, StringBuilder), previously initialized to "".
-
May 2nd, 2012, 03:45 PM
#5
Re: Text Replacing code
Hello TheGreatCthulhu,
Ok, that makes sense, so I'll go with the stringbuilder.
Thanks Again,
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|