Converting VBScript FormatString function to C# function

Posted on 2011-10-27
Last Modified: 2012-05-12
Hi Experts,

I am having below function code in VbScript, now I want to write same function in C# 2.0 code.
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))
	Set matches = nothing
	Set RegExp = nothing

	FormatString = result
End Function

Open in new window

Please suggest!!
Question by:tia_kamakshi
    LVL 22

    Expert Comment

    by:Om Prakash
    Regex are supported by C#. pl check the following link for examples. you can put above pattern it will work.
    LVL 74

    Accepted Solution

    Try this:
    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;

    Open in new window

    LVL 2

    Expert Comment

    The regular expression you are trying to match looks like the built in .NET String.Format method formatting.
    If you have the string "This {0} a {1} for the {1} {2}" and you gave it an array of string values "is", "test", and "team" you want the end result to be "This is a test for the test team". Is this correct? If this is the case you don't need to create a separate method. There are several methods that do formatting like this in .NET (String.Format, Console.WriteLine, etc). If this the formatting you are looking for here's a link for more info on the method:

    And .NET string formatting:

    string format = "This {0} a {1} for the {1} {2}";
    string[] values = string[3] {"is", "test", "team"};
    string returnValue = String.Format(format, values);

    Open in new window

    You can also just list the values in the parameters without the array with the same result:

    string format = "This {0} a {1} for the {1} {2}";
    string returnValue = String.Format(format, "is", "test", "team");

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now