Results 1 to 5 of 5

Thread: [RESOLVED] Find the Alpha Characters on Left Side of String

  1. #1

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Resolved [RESOLVED] Find the Alpha Characters on Left Side of String

    I need to come up with a method to get the alphabetical characters from a string. The strings will always have alpha characters on the left side but it will not always be the same number of them.

    Strings will look like this: "A0001", "AA001", "AB021", "ABC12", etc...

    I want to get the Alpha part of the string broken out to be used in a variable. I guess I could loop through it until I find the first numeric character, but I was hoping there would be a more efficient method, maybe Regex or something.

  2. #2

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Find the Alpha Characters on Left Side of String

    Okay, I think I got it:

    Code:
    static string GetAlphabeticalValue(string x)
            {
                return (new Regex("[A-Z]*").Match(x).Value);
            }
    This appears to work. The string will always contain upper case letters.

  3. #3

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Find the Alpha Characters on Left Side of String

    Okay, I've got this on the way now. The next step is to determine where in the alphabet the last alpha character in the string occurs.
    Code:
    public static string vAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    public static string Right(this string value, int length)
            {
                return value.Substring(value.Length - length);
            }
    
    string vAlphaIndex = vAlpha.Right(1);
    I need to find where in the string vAlphabet the character vAlphaIndex occurs at. For example if the last letter in the alpha portion of it is A, then I would need 1, B would be 2, etc.... I guess something similar to INSTR in VB.NET.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Find the Alpha Characters on Left Side of String

    As I'm not really very good with regular expressions, here's how I would have done the first part:
    Code:
    Dim alpha = New String(myString.TakeWhile(Function(ch) Char.IsLetter(ch)).ToArray()
    For the second part:
    Code:
    Dim ch = alpha.Last()
    Dim number = Convert.ToInt32(Char.ToUpper(ch)) - Convert.ToInt32("A"c) + 1

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Find the Alpha Characters on Left Side of String

    Oops! Forgot I was in C# land:
    Code:
    var alpha = new string(myString.TakeWhile(ch => char.IsLetter(ch)).ToArray();
    var ch = alpha.Last();
    var number = Convert.ToInt32(char.ToUpper(ch)) - Convert.ToInt32('A') + 1;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width