-
May 16th, 2014, 08:50 AM
#1
Thread Starter
Hyperactive Member
[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.
-
May 16th, 2014, 08:58 AM
#2
Thread Starter
Hyperactive Member
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.
-
May 16th, 2014, 09:28 AM
#3
Thread Starter
Hyperactive Member
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.
-
May 16th, 2014, 10:34 AM
#4
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
-
May 16th, 2014, 10:37 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|