Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

If we have to statically replace string like

String.Format("this is my string {0} & {1}","First","second");
But what if I get dynamically {0},{1},..{n}

How can I do it easily replace n elements with my parameters from UI?

C#
String dynString = "This is a dynamic string {0} & {1}";
String[] replaceString = new String[] { "First", "Second" };
Console.Write(String.Format(dynString, replaceString));
Console.Read();


I wrote something like this just wondering whether this will work for n when I get this replaceString with n number of elements.


Thanks.
Posted
Updated 19-Aug-12 21:02pm
v3
Comments
pradiprenushe 20-Aug-12 2:21am    
Use Arraylist with for loop or Use IEnumerable interface with foreach.

If your parameter is a collection of string means you can use it like this
ex:
C#
string[] names = { "Anders", "Eric", "Scott", "Duncan" };
            Console.Write("My string is ");
            foreach (string curr in names)
                Console.Write(curr + " ");


If you explain clearly about your parameter details and the output you going to show then I could try to show the result in that way..
 
Share this answer
 
If I understand your question correctly, you get some format string and want to check if it is valid for your parameter array?

You may go and have a look at Escaping in C#: characters, strings, string formats, keywords, identifiers[^], go to the Bonus paragraph in the string.format escaping section.

A solution may be (tweak with the number of args and with the formatting to see it work...):
C#
static void Main(string[] args)
{
    string dynString = "This is a dynamic string {0} & {1}";
    var constArgs = new object[] { "First", "Second" };
    string formatted = DynFormat(dynString, constArgs);
    Console.WriteLine(formatted ?? "<wrong format>");
}
// get all string format ids in the sequence they appear in the format string
private static IEnumerable<int> GetIds(string format)
{
    string pattern = @"\{(\d+)[^\}]*\}";
    return Regex.Matches(format ?? string.Empty, pattern, RegexOptions.Compiled)
                    .Cast<Match>()
                    .Select(m => int.Parse(m.Groups[1].Value));
}
// true  = the format matches the number of args,
// false = the format does not get sufficient args
private static bool HasSufficientArgs(string dynFormat, object[] args)
{
    var ids = GetIds(dynFormat).ToArray();
    // if no ids (no formatting), then ignore args
    // if there are ids, they must have matching args (excess args are ignored)
    return (ids.Length == 0)
           || (args != null) && (args.Length > ids.Max());
}
// returns the formatted string if there are sufficent args for the given format,
// returns null otherwise
public static string DynFormat(string dynFormat, params object[] args)
{
    return HasSufficientArgs(dynFormat, args)
           ? string.Format(dynFormat, args)
           : null;
}


Cheers
Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900