当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Split String with spaces and minus sign

问题描述:

I am trying to split a string which contains a sequence "Song Name - Artist Name". I've done similar string manipulations in PHP with relative ease as shown below

PHP:

$titledata = explode(" - ", $title);

This is what I'm trying in C#:

string[] titledata = title.Split(" - ");

And it returns the error "Cannot convert from 'string' to 'char[]'". I've tried using ToCharArray() and while it runs, it doesn't work properly. I'm not sure if it's a problem with the minus sign or the number of characters being used as the seperator.

网友答案:

The overloads that split by strings requires them to be in an array. You'd have to do this:

string[] titledata = title.Split(new[] { " - " }, StringSplitOptions.None)
分享给朋友:
您可能感兴趣的文章:
随机阅读: