Ushbu VBScript Funktsiyasini bajarish nima?

Menda VBScript funktsiyasi mavjud, u nima qiladi? Qanday qilib uni C# 2.0 yordamida soddalashtirishim mumkin?

Function FormatString(format, args)
    Dim RegExp, result

    result = format

    Set RegExp = New RegExp 

    With RegExp
        .Pattern = "\{(\d{1,2})\}"
        .IgnoreCase = False
        .Global = True
    End With

    Set matches = RegExp.Execute(result)

    For Each match In matches   
        dim index
        index = CInt(Mid(match.Value, 2, Len(match.Value) - 2))
        result = Replace(result, match.Value, args(index))
    Next
    Set matches = nothing
    Set RegExp = nothing

    FormatString = result
End Function

rahmat!

1
Men uchun VB.NETga o'xshaydi, VBScript emas - juda ko'p turli hayvonlarga.
qo'shib qo'ydi muallif Tim, manba

3 javoblar

NET-ning soddalashtirilgan versiyasiga o'xshaydi. String.Format usulidan foydalaning.

Bu shlyapa brace cheklangan erlarni egalari bilan formatlashni oladi (masalan, "{0} {1}" ) va har biri o'z navbatida args qator. Funktsional o'zgarishsiz kodni String.Format uchun almashtirishingiz mumkin.

4
qo'shib qo'ydi
Ushbu string.format bilan aloqasi yo'q. Regex naqshini moslashtirmoqda.
qo'shib qo'ydi muallif Bradley Uffner, manba
Siz to'g'ri, uzr so'rayman.
qo'shib qo'ydi muallif Bradley Uffner, manba
Iltimos, ba'zi kod namunalari bilan taklif qila olasizmi? rahmat
qo'shib qo'ydi muallif Manoj Singh, manba
@BradleyUffner, string.format ning juda soddalashtirilgan versiyasini amalga oshirish uchun regex naqshini moslashtirmoqdadir
qo'shib qo'ydi muallif Jon, manba

Belgilangan regex naqshga mos keladigan va funktsiyaga topshirilganlar ro'yxatidan boshqa bir qator bilan almashtirish uchun hamma narsaga stringni izlaydi.

Regex bilan mening (cheklangan) ko'nikmalarimga asoslanib, kirish satrida 1 yoki 2 raqamli sonlarni qidirib toping va ularni funktsiyaga uzatiladigan qatordagi qiymat bilan almashtirmoqchiman.

Here is some documentation from MSDN. http://msdn.microsoft.com/en-us/library/hs600312.aspx

It could be replaced with String.Format as documented here http://msdn.microsoft.com/en-us/library/system.string.format.aspx

Foydalanishdagi bog'langan sahifadan namuna.

DateTime dat = new DateTime(2012, 1, 17, 9, 30, 0); 
string city = "Chicago";
int temp = -16;
string output = String.Format("At {0} in {1}, the temperature was {2} degrees.",
                              dat, city, temp);
Console.WriteLine(output);
// The example displays the following output:
//    At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.   
1
qo'shib qo'ydi

Kodni C# ga o'zgartirdim

static string FormatString(string format, string[] args)
{
    System.Text.RegularExpressions.Regex RegExp;
    System.Text.RegularExpressions.MatchCollection matches;
    string result;

    result = format;

    RegExp = new System.Text.RegularExpressions.Regex(@"\{(\d{1,2})\}", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    matches = RegExp.Matches(result);

    foreach (System.Text.RegularExpressions.Match match in matches)
    {
        int index;

        index = Convert.ToInt32(match.Value.Substring(1, match.Value.Length - 1));
        result = result.Replace(match.Value, args[index]);
    }

    matches = null;
    RegExp = null;

    return result;
}

Iltimos, har qanday muammolar haqida menga xabar bering

1
qo'shib qo'ydi