简体   繁体   中英

How to get string between two specific symbols

I have a string like This: ***T1***2DAR***R1*** . I want to get this three(T1,2DAR,R1) value in three different Strings. How to decode this strings between a specific symbol like *** from a single string in VS 2015?

Use the Split method of the String class :

var values = "***T1***2DAR***R1***".Split(new string[] { "***" }, StringSplitOptions.RemoveEmptyEntries);

Edit :

This will return you a string array, you can access each value with the indexer :

string s1 = values[0]; // Will give you "T1"
string s2 = values[1]; // Will give you "2DAR"
string s3 = values[2]; // Will give you "R1"

This is where String.Split comes in handy:

string[] items = "***T1***2DAR***R1***".Split(new string[] { "***" }, StringSplitOptions.RemoveEmptyEntries)

The code above returns an array containing "T1" , "2DAR" and "R1" .

The first argument indicates the separator(s), here "***" , and the second ensures that empty strings between separators wouldn't be returned. Without it, an empty string would be returned at the beginning (before the initial *** ) and at the end of your input (after the trailing *** ).

Use the Split method like this:

string str = "***T1***2DAR***R1***";

var result = str.Split(new []{"*"}, StringSplitOptions.RemoveEmptyEntries);

This will give you an array of the individual strings.

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