Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting on a Unique Character

I want to build a comma separated list so that I can split on the comma later to get an array of the values. However, the values may have comma's in them. In fact, they may have any normal keyboard character in them (they are supplied from a user). What is a good strategy for determining a character you are sure will not collide with the values?

In case this matters in a language dependent way, I am building the "some character" separated list in C# and sending it to a browser to be split in javascript.

like image 405
Joda Maki Avatar asked Jan 20 '11 22:01

Joda Maki


People also ask

How do you split a string at a certain character?

To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do I split a string into number of substrings?

Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.

How many ways can you split a string?

Number of Ways to Split a String - LeetCode. Given a binary string s , you can split s into 3 non-empty strings s1 , s2 , and s3 where s1 + s2 + s3 = s . Return the number of ways s can be split such that the number of ones is the same in s1 , s2 , and s3 . Since the answer may be too large, return it modulo 109 + 7 .

What is the function to divide multi word string into number of substrings?

Use the JavaScript String split() to divide a string into an array of substrings by a separator. Use the second parameter ( limit ) to return a limited number of splits.


2 Answers

You could split it by a null character, and terminate your list with a double null character.

like image 44
Nick Banks Avatar answered Oct 13 '22 11:10

Nick Banks


If JavaScript is consuming the list, why not send it in the form of a JavaScript array? It already has an established and reliable method for representing a list and escaping characters.

["Value 1", "Value 2", "Escaped \"Quotes\"", "Escaped \\ Backslash"]
like image 131
Greg Avatar answered Oct 13 '22 13:10

Greg