简体   繁体   中英

Turn a string of '"a", "b", "c"' into an array of strings

Please read the specifics below: I have a string array as so:

string[] data = new string[] { "word1", "word2", "word3" };

I'm trying to put a STRING variable, which will include the "word1", "word2", "word3" data, inside of the string array. In other words, I want the string array to look as so:

SingleStringHere = "\"word1\", \"word2\", \"word3\"";
string[] data = new string[] { SingleStringHere };

The 'SingleStringHere' variable will be retrieving data off of a server, which will be used in the string array. The string array will be formatted and encrypted properly, in order to be sent in a packet through a socket.

No errors are given with the code, however, data in the 'SingleStringHere' variable is not being read as separate strings. I do NOT want to put the retrieved server data into a string array, because that will be TOO MUCH parsing!

If the strings that you receive don't contain commas, you could do something as simple as this:

string SingleStringHere = "\"word1\", \"word2\", \"word3\"";
string[] data = SingleStringHere.Replace("\"").Split(',');

Otherwise, you're going to have to do some more complex parsing. Something like this:

static void Main(string[] args)
{
      string SingleStringHere = "\"word1\", \"word2\", \"word3\"";
      string[] data = ParseSingleString(SingleStringHere);
      foreach(string s in data)
      {
            Console.WriteLine(s);
      }
}

public static string[] ParseSingleString(string singleString)
{
      List<string> multipleStrings = new List<string>();
      StringBuilder current = new StringBuilder();
      bool inQuote = false;
      for(int index = 0; index < singleString.Length; ++index) // iterate through the string
      {
            if (singleString[index] == '"')
            {
                   inQuote = !inQuote;
            }
            else if (!inQuote && singleString[index] == ',') // split at comma if not in quote
            {
                   multipleStrings.Add(current.ToString().Trim());
                   current.Clear();
            }
            else
            {
                   current.Append(singleString[index]);
            }
      }
      multipleStrings.Add(current.ToString()); // don't forget the last one
      return multipleStrings.ToArray();
}

If the strings can contain quotes, it can get trickier. That's just my rough example.

Be warned that this operation might be memory-intensive with all of the string copying and so forth (I count about 3 copies for each substring). You may be able to circumvent some of this by recording the indices of the first and last character in one of the strings, and then taking a substring on the entire singleString . Also note that List<string> has to be copied into an array before it returns. You may want to just return an IEnumerable<string> instead, or even an IList<string> . But it's late, and I think the above is sufficient for this question.

Ps I'm on a Linux machine right now without access to a C# compiler, so I apologize for any typos.

You can get array from formatted string like this

string SingleStringHere = "\"word1\", \"word2\", \"word3\"";
String[] arr = SingleStringHere.Split(',');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM