Unique character division

I want to create a comma-delimited list so that later I can split into a comma to get an array of values. However, values โ€‹โ€‹may have a comma in them. In fact, they can have any ordinary keyboard character in them (they are provided by the user). What is a good strategy to identify a character you are sure will not run into values?

In case it matters at the language level, I create a โ€œseparate characterโ€ in the C # list and send it to the browser, which should be split into javascript.

+7
source share
7 answers

You can split it into a null character and end your list with a double null character.

+1
source

If JavaScript uses a list, why not send it as a JavaScript array? It already has an established and reliable way of presenting a list and escaping characters.

["Value 1", "Value 2", "Escaped \"Quotes\"", "Escaped \\ Backslash"] 
+3
source

I always use | but if you still think it can contain it, you can use combinations like @ | @. For example:

 "string one@ |@string two@ |@ ...@ |@last string" 
0
source

Eric S. Raymond has written a book about this to find useful . It is aimed at Unix users, but should still apply.

As for your question, if you have commas inside the cells, you will need some form of escape. Using \, is the standard way, but you will also have to avoid the slash, which are also common.

Alternatively, use another character, such as a pipe (|), tab, or something else of your choice. If users need to work with data using a spreadsheet program, you can usually add filtering rules to separate the cells on the separator of your choice. If this is a concern, it is best to choose a delimiter that users can easily enter, which excludes nul char, among others.

You can also use quoting:

 "value1", "value2", "etc" 

In this case, you will only need to avoid quotation marks (and slashes). This should also be accepted by spreadsheets taking into account the correct filter parameters.

0
source

If there is potential for any separator character to appear in your lines, then I would suggest you write a script element to your output using the javascript array definition. For example:

 <script> var myVars=new Array(); myVars[0]="abc|@123$"; myVars[1]="123*456"; myVars[2]="blah|blah"; </script> 

Your javascript can then reference this array

Doing this also avoids the need to create a comma-delimited string from an array of C # strings.

The only thing I can think of is strings containing quotation marks, in which case you will have to escape from them in C # when writing them to myVars output.

0
source

There are several ways to do this. The first is to select a separator character, which is usually not entered from the keyboard. NULL or TAB are usually good. Secondly, using a character sequence as a separator, Excel CSV files are a good example where cell values โ€‹โ€‹are defined with commas quotes that separate cells.
The answer depends on whether you want to reinvent the wheel or not.

0
source

There is an RFC that documents the CSV format. Follow the standards and you will avoid reinventing the wheel and creating a mess for the next guy to come and support your code. It's nice that there are libraries available for importing / exporting CSV for almost any platform you can imagine.

However, if you serialize the data to be sent to the browser, JSON is really the way to go, and it is also documented in the RFC and you can get libraries for almost any platform, such as JSON.NET

0
source

All Articles