Skip to content
Advertisement

How can I split a string without knowing the split characters a-priori?

For my project I have to read various input graphs. Unfortunately, the input edges have not the same format. Some of them are comma-separated, others are tab-separated, etc. For example:

File 1:

JavaScript

File 2

JavaScript

Rather than handling each case separately, I would like to automatically detect the split characters. Currently I have developed the following solution:

JavaScript

Basically I pick the first row and select all the non-numeric characters, then I use the generated substring as split characters. Is there a cleaner way to perform this?

Advertisement

Answer

Split the string on \D+ which means one or more non-digit characters.

Demo:

JavaScript

Output:

JavaScript
Advertisement