Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to extract string from a separator in c#

Tags:

c#

asp.net

Please I have a string such as this RemoteAuthorizationTestScope|Start:En Attente Validation|:001|:01195|21/01/2015

I can extract the string from the | symbol using this code

var userInfo="RemoteAuthorizationTestScope|Start:En Attente Validation|:001|:01195|21/01/2015"
var entries=userInfo.Split('|');

however, there are also text such "Start:En Attente Validation" in the string that needs to be separated, how do I achieve this so that I can have the string separated in this manner.

RemoteAuthorizationTestScope
Start
En Attente Validation
001 
01195
21/01/2015
like image 817
user989865 Avatar asked Jan 21 '15 16:01

user989865


People also ask

How do you separate a string from a delimiter?

You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma-separated String on a comma, breaking a pipe-delimited String on a pipe, or splitting a pipe-delimited String on a pipe.

How can I split a string in C?

Splitting a string using strtok() in C In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.

How do you separate data from a string?

Use the string split in Java method against the string that needs to be divided and provide the separator as an argument. In this Java split string by delimiter case, the separator is a comma (,) and the result of the Java split string by comma operation will give you an array split.

How do I split a string without a separator?

Q #4) How to split a string in Java without delimiter or How to split each character in Java? Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.


1 Answers

You can specify multiple characters to split and also remove empty entries. Use the String.Split overload like:

var entries = userInfo.Split(new []{'|', ':'}, StringSplitOptions.RemoveEmptyEntries);

That will give you

RemoteAuthorizationTestScope
Start
En Attente Validation
001 
01195
21/01/2015
like image 81
Habib Avatar answered Oct 04 '22 06:10

Habib