2

It is very basic question but i am not sure why it is not working. I have code where 'And' can be written in any of the ways 'And', 'and', etc. and i want to replace it with ','

I tried this:

and.Replace("and".ToUpper(),",");

but this is not working, any other way to do this or make it work?

horgh
  • 17,918
  • 22
  • 68
  • 123
NoviceMe
  • 3,126
  • 11
  • 57
  • 117
  • 1
    That is equivalent to `and.Replace("AND", ",")`. In any case, look at Regex.Replace and case-insensitivity mode. –  Sep 21 '12 at 00:48
  • 1
    As @pst mentioned, you can use regex too: `var regex = new Regex( "camel", RegexOptions.IgnoreCase ); var newSentence = regex.Replace( sentence, "horse" ); ` code take from: http://stackoverflow.com/questions/6025560/how-to-ignore-case-in-string-replace – Jack Sep 21 '12 at 00:50
  • You can also use the "(?i)and" option in the search pattern. This way you can use the static `Replace()` method since you don't need to use the `RegexOption.IgnoreCase` enum. I gave some code below. – Matt Klein Sep 21 '12 at 01:10
  • You should consider that sentence can contain words like 'sand'. The Regex will replace them as well. That's not correct. – horgh Sep 21 '12 at 01:24
  • I've made an StringExtensions project that provides an overload of Replace taking a ComparisonType, see: http://stringextensions.codeplex.com/SourceControl/changeset/view/66ba3d678467#src%2fStringExtensions%2fCommonStringExtensions.Replace.cs Now, you can simply say: `mystring.Replace("and", ",", StringComparison.InvariantIgnoreCase);` which is way more performant than any regex solution – Polity Sep 21 '12 at 02:22

5 Answers5

6

You should check out the Regex class

http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx

using System.Text.RegularExpressions;

Regex re = new Regex("\band\b", RegexOptions.IgnoreCase);

string and = "This is my input string with and string in between.";

re.Replace(and, ",");
Neverever
  • 15,890
  • 3
  • 32
  • 50
  • Good use of `\b` (+1). I prefer to use the Regex static methods, but that's mostly just preference .. –  Sep 21 '12 at 02:48
2
words = words.Replace("AND", ",")
             .Replace("and", ",");

Or use RegEx.

lahsrah
  • 9,013
  • 5
  • 37
  • 67
  • 1
    He haven't mentioned in fact, but I think that you must to consider `And`, `aNd` `ANd` inputs etc. So, converts the input string and search string to even case and do the replace, or use only regex. – Jack Sep 21 '12 at 00:57
  • @Jack Yea that was just an example. I mean he could continue to chain the .Replace methods on or your RegEx as I mentioned. Since its just a limited number of combinations .Replace isn't that bad. – lahsrah Sep 21 '12 at 01:00
2

The Replace method returns a string where the replacement is visible. It does not modify the original string. You should try something along the lines of

and = and.Replace("and",",");

You can do this for all variations of "and" you may encounter, or as other answers have suggested, you could use a regex.

K Mehta
  • 10,323
  • 4
  • 46
  • 76
2

I guess you should take care if some words contain and, say "this is sand and sea". The word "sand" must not be influenced by the replacement.

string and = "this is sand and sea";

//here you should probably add those delimiters that may occur near your "and"
//this substitution is not universal and will omit smth like this " and, " 
string[] delimiters = new string[] { " " }; 

//it result in: "this is sand , sea"
and = string.Join(" ", 
                  and.Split(delimiters,  
                            StringSplitOptions.RemoveEmptyEntries)
                     .Select(s => s.Length == 3 && s.ToUpper().Equals("AND") 
                                     ? "," 
                                     : s));

I would also add smth like this:

and = and.Replace(" , ", ", ");

So, the output:

this is sand, sea
horgh
  • 17,918
  • 22
  • 68
  • 123
0

try this way to use the static Regex.Replace() method:

and = System.Text.RegularExpressions.Regex.Replace(and,"(?i)and",",");

The "(?i)" causes the following text search to be case-insensitive.

http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx

http://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.100).aspx

Matt Klein
  • 7,856
  • 6
  • 45
  • 46