In need of examples to determine if an alphanumeric string is between two other alphanumeric strings in C#

I have a project where I need to convert the current code in VBScript to C#. The part I’m stuck on is the VBScript code compares an alphanumeric medical procedure code to see if it’s between two other alphanumeric procedure codes. Here’s the VBScript code:

If ProcCode >= "A4000" And ProcCode <= "A7999" Then
   [do stuff here]
End If

If I have a procedure code A5150, I need a C# code that does the same as the VBScript code. I’m not finding what I’m looking for on the internet. Any advice or URL of where I can find this solution?

3

I don’t have enough rep to just comment yet. @Mike Christensen’s comment is the most direct answer. Use the built in String.Compare() method. You can find the documentation here:

Microsoft String.Compare

Note that String.Compare(x, y) is null safe — stringVar1.Compare(stringVar2) is not null safe. But just because it will compile and run without error, you will want to include test cases to ensure you get expected output.

1

You can compare strings in C# the same way you did in VB. All languages, at least those known to me, compare strings the same way by default.

If you are using some kind of complex alphanumeric encoding, then this library function may be useful for you:

using System.Runtime.InteropServices;

[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
static extern int StrCmpLogicalW(string psz1, string psz2);

var ProcCode = "A5150";

if (StrCmpLogicalW(ProcCode, "A4000") >= 0 &&
    StrCmpLogicalW(ProcCode, "A7999") <= 0)
    /*do stuff here*/;

I would define my own type, with implicit conversion to string, so you can easily assign it and overload >= and <= operator (and you can override more operators if needed).

To compare two codes I assumed such logic – remove A at the beginning and compare numeric values.

Here’s sample implementation:

public class MyCode
{
    private string _code;

    private MyCode(string code) => _code = code;

    public static implicit operator MyCode(string code)
    {
        return new MyCode(code);
    }

    public static bool operator <=(MyCode code1, MyCode code2)
    {
        return GetIntFromCode(code1) <= GetIntFromCode(code2);
    }

    public static bool operator >=(MyCode code1, MyCode code2)
    {
        return GetIntFromCode(code1) >= GetIntFromCode(code2);
    }

    private static int GetIntFromCode(MyCode code) => int.Parse(code._code.TrimStart('A'));
}

Example usage:

MyCode code = "A4999";

var r1 = "A4000" <= code; // true
var r2 = "A4000" >= code; // false
var r3 = "A5000" <= code; // false
var r4 = "A5000" >= code; // true

Update

Alternatively, you can define some extension methods on string (with the same comparison logic), if you would like this approach:

public static class StringCodeComparer
{
    public static bool IsLessOrEqualTo(this string x, string y)
    {
        return GetIntFromString(x).CompareTo(GetIntFromString(y)) <= 0;
    }

    public static bool IsGreaterOrEqualTo(this string x, string y)
    {
        return GetIntFromString(x).CompareTo(GetIntFromString(y)) >= 0;
    }

    public static int CompareCode(this string x, string y)
    {
        return GetIntFromString(x).CompareTo(GetIntFromString(y));
    }

    private static int GetIntFromString(string code) => int.Parse(code.TrimStart('A'));
}

And then usage would be:

var result1 = "A4000".IsGreaterOrEqualTo("A4999"); // false
var result2 = "A5000".IsGreaterOrEqualTo("A4999"); // true
var result3 = "A4000".IsLessOrEqualTo("A4999"); // true
var result4 = "A5000".IsLessOrEqualTo("A4999"); // false

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật