0

I have the list and need to make them uppercase if they contain specific letter.

I tried to use mean.ToString().ToUpper().Contains("g"); but it doesn't work.

    static void Main(string[] args)
    {           

        List<string> mean = new List<string>() { "dog", "smile", "cat", 
        "bat", "giraffe"};
        // e.g. values containing letter "g" have to be changed to uppercase
        // "dog" and "giraffe" should be changed to UPPERCASE


        foreach (string s in mean)
        {
            Console.WriteLine(s);
        }
    }
Bakai Ray
  • 13
  • 5

3 Answers3

1

You can use Linq with a simple ternary to accomplish this:

static void Main(string[] args)
    {
        List<string> mean = new List<string>() { "dog", "smile", "cat", "bat", "giraffe"};
        mean.RemoveAll(item => item.Contains("t"));
        mean = mean.Select(item => item.Contains("g") ? item.ToUpper() : item).ToList();

        foreach (string s in mean)
        {
            Console.WriteLine(s);
        }
    }

The Select method will take each element in the list and perform an operation on it. In this case, we only want to perform the operation if the element contains "g". That's where the ternary comes in. If the item contains "g", we return item.ToUpper(), and if not we just return item. Since the output of a select statement is an IEnumerable<string>, we need to call the ToList() method on it in order to assign the value back to mean.

The output of the above code is:

DOG smile GIRAFFE

Atuw
  • 26
  • 2
  • `mean = mean.ConvertAll(item => item.Contains("g") ? item.ToUpper() : item);` is this correct use of the method? – Bakai Ray Oct 20 '19 at 19:06
  • Yes, that will work. Although, `Select` is the favored method, as it will work with IEnumerables as well, which you will find come up a lot in c#. If you really want to remove the dependency on Linq, you can use `ConvertAll` to get the same result. See [this Stackoverflow question](https://stackoverflow.com/questions/1571819/difference-between-select-and-convertall-in-c-sharp) for more info – Atuw Oct 20 '19 at 19:13
  • Also, if you expect you might get an uppercase letter, make sure to add `ToLower()` before `Contains("g")`. – Atuw Oct 20 '19 at 19:28
0

How you expect it to match a LowerCase g after you already converted it to UpperCase G

mean.ToString().ToUpper().Contains("g");

Should be

mean.ToString().ToUpper().Contains("G");

or even better

mean.IndexOf("g", StringComparison.InvariantCultureIgnoreCase) >= 0
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

in the foreach you don't use mean.ToString().ToUpper().Contains("g"); instead you use s as the mean is the list, so yes, it'll not work.

Also, .ToString().ToUpper().Contains("g") the ToUpper() here is going to make the string UpperCase so .Contains("g") sould be uppercase as well.

you can do this :

foreach (string s in mean)
{
    if (s.ToLower().Contains("g"))
        Console.WriteLine(s.ToUpper());
}

or if you like Linq:

// Linq

var result = mean.Where(e => e.ToLower().Contains("g")).Select(x => x.ToUpper()).ToList();

foreach (var s in result)
{
    Console.WriteLine(s);
}
iSR5
  • 3,274
  • 2
  • 14
  • 13