Assim
Assim

Reputation: 45

C# How to get the length of chars in a string

I have a []string retry and I have some strings inside of it let's say: a 2 , a 3 , h 9, asd 123 and so on. They all have intervals between the letter and the integer. Now I need to get how long is the letter part (in this case "qwe 2" the letter length is 3 and the integer part is 2). I'm later using .substring from the specified index at which the letter part finishes and the integer one starts.

string[] s = new string[5];
List<string> retry = new List<string>();
int max = 1;

for (int i = 0; i < s.Length; i++)
{
    s[i] = Console.ReadLine();
}

Console.WriteLine(" ");
Array.Sort(s);

for (int j = 0; j < s.Length; j++)
{
    if (j > 0)
    {
        if (s[j] == s[j - 1])
        {
            max++;

            if (retry.Any(x => x.Contains(s[j])))
            {
                retry.Add(s[j] + " " + max);
            }
            else
            {
                if (j <= s.Length - 1)
                {
                    if (s[j-1] != s[j])
                    {
                        retry.Add(s[j] + " " + max);
                    }
                    else
                    {
                        retry.Add(s[j] + " " + max);
                    }
                }
            }
        }
        else
        {
            max = 1;
        }
    }
}

for (int j = 0; j < retry.ToArray().Length; j++)
{
    for (int k = j + 1; k < retry.ToArray().Length; k++)
    {
        var a1=retry[j].Substring(0,1);
        var a2 = retry[k].Substring(0,1);

        if(a1==a2)
        {
            var b1 = retry[j].Substring(2);
            var b2 = retry[k].Substring(2);

            if(int.Parse(b1)>int.Parse(b2))
            {
                retry.Remove(a2 + " "+ b2);
            }
            else
            {
                retry.Remove(a1 + " " + b1);
            }
        }
    }

    Console.WriteLine(retry[j]);
}

Console.ReadKey();

This only works for 1 letter.

Upvotes: 3

Views: 11342

Answers (3)

nura
nura

Reputation: 79

The code below should get the result as expected:

string [] entries =  {"xyz 1","q 2","poiuy 4"};
for(int i=0;i<entries.Length;i++)
{
    var parts = entries[i].Split(' ');
    var txtCount = parts[0].Length;
    Console.WriteLine(String.Format("{0} {1}", txtCount, parts[1]));
}

Upvotes: 2

AustinWBryan
AustinWBryan

Reputation: 3327

This iterates through all of the strings and counts the length of the chars, and stores the value of the number, as well as its index. Note that this information is lost upon each iteration, but I'll leave it to you to worry about storing that if you need that information for longer than the duration of an iteration.

int letterLength = 0, number = 0, index = 0;
string[] testString = { "a 2", "a 3", "h 9", "asd 123", "sw", "swa23", "swag 2464" };

foreach (string str in testString)
{
    letterLength = 0;
    index = -1;
    int i = 0;

    for (; i < str.Length; i++)
    {
        char c = str[i];
        if (Char.IsLetter(c)) letterLength++;
        else if (Char.IsNumber(c)) break;
    }

    StringBuilder strBuilder = new StringBuilder();

    for (; i < str.Length; i++)
    {
        char c = str[i];

        if (index == -1) index  = str.IndexOf(c);
        strBuilder.Append(c);

        number = Int32.Parse(strBuilder.ToString());
    }

    Console.WriteLine($"String: {str}\nLetter Length: {letterLength} Number: {number} Index: {index}\n");
}

Output

Upvotes: 0

Matt Searles
Matt Searles

Reputation: 2765

How about...

string[] test = new [] { "qwe 2", "a 2", "b 3", "asd 123" };

foreach (var s in test)
{
    var lastLetterIndex = Array.FindLastIndex(s.ToCharArray(), Char.IsLetter);
    var lastNumberIndex = Array.FindLastIndex(s.ToCharArray(), Char.IsNumber);
    Console.WriteLine(s);
    Console.WriteLine("Letter Length : " + (lastLetterIndex + 1));
    Console.WriteLine("Number Length : " + (lastNumberIndex - lastLetterIndex));
}
Console.ReadKey();

Upvotes: 1

Related Questions