none
String.Format opposite RRS feed

  • Question

  • Hi everybody,

    How are you ?

    I'm trying to build an extension method for the class String that does the opposite of String.Format.

    String.Format("My name is {0} {1}", "John", "Doe")

    This will return "My name is John Doe".

    Now let's imagine the extension method :

    // Extension method
    public static string[] Deformat(this string s, string input);
    
    // Example of use
    var s = "My name is John Doe";
    string[] names = s.Deformat("My name is {0} {1}");

    I'd like names to contain a string array with "John" and "Doe".

    Any idea about how to do that ? It looks like RegEx is a good approach but I has been working on that for hours unsuccessfully.

    Thank you in advance for your help.

    Tuesday, June 5, 2018 9:12 PM

All replies

  • I'm curious how this would work say if the string was "Mary Ann Jones" and other permutations of names that are more than two tokens how you are showing in the example you would be missing part of the name.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, June 5, 2018 10:39 PM
    Moderator
  • This answer not bad but I rewrite,

    Extendted method,

    	/// <summary>
    	/// Reversing class
    	/// </summary>
    	public static class ReversingClass
    	{
    		/// <summary>
    		/// String.Format reverser
    		/// </summary>
    		/// <param name="string">string value</param>
    		/// <param name="template">format template</param>
    		/// <returns>List<string></returns>
    		public static List<string> Deformat(this string @string, string template)
    		{
    			// create matches list
    			List<string> matches = null;
    
    			// replace slashes
    			template = Regex.Replace(template, @"[\\\^\$\.\|\?\*\+\(\)]", p => "\\" + p.Value);
    
    			// replace index numbers
    			var pattern = "^" + Regex.Replace(template, @"\{[0-9]+\}", "(.*?)") + "$";
    
    			// get matches
    			var match = new Regex(pattern).Match(@string);
    
    			// if there is not any match, return null matches
    			if (match.Groups.Count <= 0) return matches;
    
    			// set list
    			matches = new List<string>();
    
    			// loop with match count
    			for (var i = 1; i < match.Groups.Count; i++)
    			{
    				// add to matches list
    				matches.Add(match.Groups[i].Value);
    			}
    
    			return matches;
    		}
    	}

    Usage, 

    var myString = String.Format("My name is {0} {1}", "John", "Doe");
    
    var parsedString = myString.Deformat("My name is {0} {1}");

    Good luck :)


    TR| Sorunuzun yanıtı bu ise "Yanıt Olarak Öner" olarak işaretleyin, eğer faydalı bir yorum ise "Oy Ver"erek forumun işleyişine katkıda bulunabilirsiniz. EN| If this is the answer to your problem, mark "Propose as Answer" and if it is helpful, you can contribute to the workig of the forum by "Voting".


    Wednesday, June 6, 2018 12:47 AM
  • Hi NicolasC,

    Egoist's solution is good for you, it can work in my test:

            private void button1_Click(object sender, EventArgs e)
            {
                var myString = String.Format("My name is {0} {1}", "John", "Doe");
                var parsedString = myString.Deformat("My name is {0} {1}");
    
                richTextBox1.AppendText(parsedString[0] + Environment.NewLine);
                richTextBox1.AppendText(parsedString[1]);
            }

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 6, 2018 6:37 AM