jeyejow
jeyejow

Reputation: 429

C# read string from back to end

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.

Upvotes: 4

Views: 3048

Answers (5)

haku-kiro
haku-kiro

Reputation: 66

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#.

Upvotes: 0

Wheels73
Wheels73

Reputation: 2890

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;
        }
    }

Upvotes: 0

InBetween
InBetween

Reputation: 32760

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());

Upvotes: 3

Wagdi
Wagdi

Reputation: 119

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

Upvotes: 0

user7138697
user7138697

Reputation:

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

Upvotes: 0

Related Questions