none
Add a quote to a comma separated string RRS feed

  • Question

  • Hi,

    I have below string:

    string from_form = "BH, US, IN, UK";


    I want to know how can I convert it to:

    string to_database = "BH", "US, "IN, "UK";

    I tried below code but it's giving me a space before the quote:

    string test_list = "'" + String.Join("','", from_form + "'";


    Thanks,

    Jassim

    Tuesday, August 9, 2016 8:11 AM

Answers

  • Hi Jassim,

    In this approach you can turn your user input into a collection that can be dynamically appended to, and turned back into your desired "to_database" format containing quotes.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Retrieve values from form
                string from_form = "BH, US, IN, UK";
    
                // Remove whitespace and convert to list
                List<string> from_form_list = from_form.Replace(" ", string.Empty).Split(',').ToList();
    
                // Dynamically add or remove to your list if desired
                from_form_list.Add("CA");
                from_form_list.Remove("IN");
    
                // Convert back into comma delimited string
                StringBuilder sb = new StringBuilder();
    
                for(int i = 0; i < from_form_list.Count; i++)
                {
                    sb.Append('"');
                    sb.Append(from_form_list[i]);
                    sb.Append('"');
    
                    // If last item in collection do not append comma
                    if (i != from_form_list.Count - 1)
                        sb.Append(", ");
                }
    
                // Get new string from strinbuilder
                string to_database = sb.ToString();
    
                // Print results
                Console.WriteLine(to_database);
    
                Console.ReadLine();
            }
        }
    }

    Hope that helps, best of luck.


    Tuesday, August 9, 2016 11:57 AM

All replies

  • Hi,

    you can put quote signs inside a string by 2 ways:

    a) When you have the escape characters, then you can escape it: 

    string from_form = "\"BH\", \"US\", \"IN\", \"UK\"";

    b) You can also do it without escape characters. Then you have to use the @ sign in front of the string and in that case two quotes are used to place one quote:

    string from_form = @"""BH"", ""US"", ""IN"", UK""";

    With kind regards,

    Konrad

    Tuesday, August 9, 2016 8:30 AM
  • but how can I dynamically do that.

    I am using DevExpress Lookup and it returns the string from the selected countries like this:

    AE, JP, US, BH, GB

    Tuesday, August 9, 2016 9:10 AM
  • Hi,

    I am not sure that I really understood you, but you could do something like this to just add the quotes:

    string source = "BH, US, IN, UK";
    string result = "\"" + source.Replace(", ", "\", \"") + "\"";

    Or did you try to get a list / array of single strings (so that you simply displayed a comma separated list of single strings instead of one string with quotes?)

    Then you might want to try something like

    string source = "BH, US, IN, UK";
    string[] result = source.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);

    With kind regards,

    Konrad

    Tuesday, August 9, 2016 9:34 AM
  • Hi Jassim,

    In this approach you can turn your user input into a collection that can be dynamically appended to, and turned back into your desired "to_database" format containing quotes.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Retrieve values from form
                string from_form = "BH, US, IN, UK";
    
                // Remove whitespace and convert to list
                List<string> from_form_list = from_form.Replace(" ", string.Empty).Split(',').ToList();
    
                // Dynamically add or remove to your list if desired
                from_form_list.Add("CA");
                from_form_list.Remove("IN");
    
                // Convert back into comma delimited string
                StringBuilder sb = new StringBuilder();
    
                for(int i = 0; i < from_form_list.Count; i++)
                {
                    sb.Append('"');
                    sb.Append(from_form_list[i]);
                    sb.Append('"');
    
                    // If last item in collection do not append comma
                    if (i != from_form_list.Count - 1)
                        sb.Append(", ");
                }
    
                // Get new string from strinbuilder
                string to_database = sb.ToString();
    
                // Print results
                Console.WriteLine(to_database);
    
                Console.ReadLine();
            }
        }
    }

    Hope that helps, best of luck.


    Tuesday, August 9, 2016 11:57 AM