4

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.

jeyejow
  • 419
  • 1
  • 5
  • 15
  • 1
    Please edit with the code that you wrote to solve this, even if the code does not work. – Sergey Kalinichenko Mar 30 '17 at 16:00
  • Do you know for certain what the other non-numberic characters are? Meaning, can you guarantee they are all lowercase a-z letters? – maccettura Mar 30 '17 at 16:05
  • 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 Mar 30 '17 at 16:08

5 Answers5

3

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());
InBetween
  • 32,319
  • 3
  • 50
  • 90
0

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

Community
  • 1
  • 1
  • ok, string rotation is solve and ty for that! Now i only need the part were we count character by character and see if its a number, is so, then keep counting till we reach the end of the string or until we find a character, and save thoes numbers in another string! – jeyejow Mar 30 '17 at 16:06
  • 1
    Please do not use that answer because it is incorrect; it does not account for combining characters, surrogates, diacritics, and other situations. Even if it works for the exact string you posted, it does not work in the general case. – Dour High Arch Mar 30 '17 at 16:08
0
string str = "3333333a224444";
var reversedStr = str.Reverse();
string result= new String(reversedStr.TakeWhile(Char.IsDigit).ToArray());
Wagdi
  • 119
  • 6
0

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;
        }
    }
Wheels73
  • 2,850
  • 1
  • 11
  • 20
  • please dont just downvote... please share?! I dont understand? This produces the correct output with each string in OP's post. What have i missed? – Wheels73 Mar 30 '17 at 16:10
  • Its probably due to two reasons: 1) the clunky `(Int32.TryParse(letter.ToString(), out number)`... thats simply horrendous, `char.IsDigit(letter)` is the better choice. 2) String concatenation `string.Format("{0}{1}", letter, newString)`... its completely inneficient and unnecessary; just build a `char` array and create the `string` at the end. – InBetween Mar 30 '17 at 16:18
  • @ Yes, if he makes the changes you say to his awnser it would be much better. I made some changes and its working just nice! Thanks to all that helped – jeyejow Mar 30 '17 at 16:21
  • @InBetween - Thank you for you feedback... I appreciate it. – Wheels73 Mar 30 '17 at 16:41
0
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#.

Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
haku-kiro
  • 66
  • 3