简体   繁体   中英

Split() string except for certain character combination

I want something like:

"aaaXaaaXaaaXaaaYXaaa".Split('X');

but want it to ignore 'YX'.

Of course I can simply loop and correct for it. But is there a built-in method for that?

You can use a regular expression with a negative lookbehind:

string[] result = Regex.Split(s, "(?<!Y)X");

See it working online: ideone

More information about lookarounds: Lookahead and Lookbehind Zero-Width Assertions

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