How to find the Index of All Uppercase Character in a String and return it's value in C#

MERUN KUMAR MAITY 511 Reputation points
2022-10-04T19:19:29.37+00:00

Hi, I have C# application I need to find the Index of All Uppercase Character in a String and return it's value.

What I tried is in C# :

 static void Main(string[] args)  
        {  
            string spam = "ConexantSmartAudio HD";  
  
            var indexes = spam  
                          .Select((chr, index) => (chr, index))  
                          .Where(tuple => Char.IsUpper(tuple.chr))  
                          .Select(tuple => tuple.index);  
  
            foreach (var index in indexes)  
            {  
                Console.WriteLine(index);  
            }  
        }  

by this code I can successfully print the index of all uppercase character in a String. But I need to return the value because I use this in a WPF converter where returning value is must for XAML binding.

My approach is, to return the indexes by using an array, first store the indexes in an array and then pass it. But I don't know how?

By the way any other clean code of finding Uppercase index is also welcome.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,100 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,016 Reputation points
    2022-10-04T22:45:55.687+00:00

    Here is another take. Try it here.

    using System;  
    using System.Linq;  
      
    namespace ConsoleApp1;  
      
    internal class Program  
    {  
        static void Main(string[] args)  
        {  
            var text = "ConexantSmartAudio HD";  
            var result = text.Select((value, index)   
                    => new UpperCaseItem(value, index, char.IsUpper(value)))  
                .Where(x => x.IsUpper);  
      
            foreach (var item in result)  
            {  
                Console.WriteLine($"{item.Index, -5:D3}{item.Value}");  
            }  
      
            Console.ReadLine();  
        }  
    }  
      
    public class UpperCaseItem  
    {  
        public char Value { get; }  
        public int Index { get; }  
        public bool IsUpper { get; }  
      
        public UpperCaseItem(char value, int index, bool isUpper)  
        {  
            Value = value;  
            Index = index;  
            IsUpper = isUpper;  
        }  
    }  
    

2 additional answers

Sort by: Most helpful
  1. Viorel 110.8K Reputation points
    2022-10-04T19:42:20.533+00:00

    If you already know that you need an array, then add int [ ] array = indexes.ToArray( );.


  2. AgaveJoe 25,866 Reputation points
    2022-10-04T21:45:48.607+00:00

    It might be easier to help you if you explain the user case rather than the solution.

    A string is just an array. Use a for loop to find the capital letters indexes and add the index to an list. Use the indexes to find the capital letters in the original string.

    string spam = "ConexantSmartAudio HD";  
    List<int> idx = new List<int>();  
      
    for (int i = 0; i < spam.Length; i++)  
    {  
        if(spam[i] >= 'A' & spam[i] <= 'Z')  
        {  
            idx.Add(i);  
        }  
    }  
      
    foreach(int i in idx)  
    {  
        Console.WriteLine($"Index: {i}\t Value: {spam[i]}");  
    }  
    

    Results

    Index: 0         Value: C  
    Index: 8         Value: S  
    Index: 13        Value: A  
    Index: 19        Value: H  
    Index: 20        Value: D  
    

    Fonts are related to the application that displays the text which is unknown at this time.

    0 comments No comments