C # reading string from start to end

I have a string. For example:

string a = "abcdef098403248";

      

My goal, knowing that the string ends with a number (otherwise the following will happen :) MessageBox.Show("String doesnt end in a number");

, I want to read the string from end to beginning, and store the values ​​in another string.Now the problem is only that I can only store in these new string numbers and when I read string a

, while I am counting from end to end, if I find a character that is not a number, I stop reading and storing the previous number found on the new line. The code output should look something like this:

string a = "aBcdef3213BBBBB0913456";
//part were i read the string from back to end
string finalString = "0913456";

      

As you can see there, I am storing numbers from left to right, but I want to read them from right to left.

Another example of what I want:

string a = "aaaaa3a224444";
// part were i read the string from back to end
string finalString = "224444";

      

Or:

string a = "3333333a224444";
// part were i read the string from back to end
string finalString = "224444";

      

No matter, thanks.

+3


source to share


5 answers


Stack<char>

- your friend:



var stack = new Stack<char>();

foreach (var c in a.Reverse())
{
    if (!char.IsDigit(c))
        break;

    stack.Push(c);
}

return new string(stack.ToArray());

      

+3


source


Reverse the line using the following function.

Take the number the wrong way - twist it back.



public static string Reverse( string s )
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
}

      

Taken from: Best Way to Change a String

0


source


string str = "3333333a224444";
var reversedStr = str.Reverse();
string result= new String(reversedStr.TakeWhile(Char.IsDigit).ToArray());

      

0


source


string a = "saffsa1ad12314";
string finalString = string.Empty;

char[] chars = a.ToCharArray();

for (int i = chars.Length -1; i >= 0; i--)
{
    if (char.IsDigit(chars[i]))
    {
        finalString += chars[i];
    }
    else
    {
        break; //so you dont get a number after a letter
        //could put your mbox here
    }
}
//Now you just have to reverse the string
char[] revMe = finalString.ToCharArray();
Array.Reverse(revMe);
finalString = string.Empty;

foreach (char x in revMe)
{
    finalString += x;
}


Console.WriteLine(finalString);
//outputs: 12314

      

This question feels awful homework - but here's a lot of verbosity about the solution to your problem. Alternatively, you can read regex in C #.

0


source


I came up with this. Not as elegant as others. It adds numbers to the string until it encounters the letter again, and then stops.

    string a = "aBcdef3213BBBBB0913456";

    var charList = a.ToCharArray();

    string newString = string.Empty;

    foreach (var letter in charList.Reverse())
    {
        var number = 0;

        if (Int32.TryParse(letter.ToString(), out number))
        {
            newString = string.Format("{0}{1}", letter, newString);
        }
        else
        {
            break;
        }
    }

      

-1


source







All Articles