4
votes

I have a string. For example :

string a = "abcdef098403248";

My goal is, knowing that the string ends with a number (else something like this will happen : MessageBox.Show("String doesnt end in a number"); ), i want to read the string, starting from the end to the begining, and store the values in another string. Now the only problem is, i can only store in that new string numbers, and when i read the string a , while i count from the back to end if i find a character that isnt a number, i stop reading and store the previous numbers found in the new string. The code output should look somthing like this:

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

As you see there, i store the 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";

Regardless, thanks.

5
Please edit with the code that you wrote to solve this, even if the code does not work. - Sergey Kalinichenko
Do you know for certain what the other non-numberic characters are? Meaning, can you guarantee they are all lowercase a-z letters? - maccettura
no, all i know is that they are not number, my guess would be have an array that have all number (0 to 9) and see if the character we find while searching is equal or diferent to annything on that arrray, maybe a for cycle for the character - jeyejow

5 Answers

3
votes

Stack<char> is 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());
0
votes

Reverse the string using the function below.

Grab the number the wrong way around - spin it back.

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

Taken from : Best way to reverse a string

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

I came up with this. Not as elegant as others. It adds the numbers to the string until it encounters a letter again 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;
        }
    }
0
votes
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 awfully homeworky - but here is a very verbose way of solving your problem. Alternatively you can read up on regex in c#.