user2615353
user2615353

Reputation: 33

Split a string by a character

I have this string here:

String FileNameOrginal = "lighthouse-126.jpg";

and I am trying to split the string into 2, seperating it with "-"

I have tried the following, but I get a syntax error on the Spilt:

String FileNameOrginal = drProduct["productHTML"].ToString();
string[] strDataArray = FileNameOrginal.Split("-");

Please Help, I do not understand what I am doing wrong.

Upvotes: 3

Views: 92

Answers (4)

bhupesh bhatt
bhupesh bhatt

Reputation: 3

  string FileNameOrginal = "lighthouse-126.jpg";
        string file1 = FileNameOrginal.Split('-')[0];
        string file2 = FileNameOrginal.Split('-')[1];

Upvotes: 0

K Griffiths
K Griffiths

Reputation: 21

Instead of:

string[] strDataArray = FileNameOrginal.Split("-");

Try

string[] strDataArray = FileNameOrginal.Split('-');

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

So the issue is that you need an array for an input, like this:

string[] strDataArray = FileNameOrginal.Split(
    new string[] { "-" },
    StringSplitOptions.None);

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

You just need a character instead of string:

string[] strDataArray = FileNameOrginal.Split('-');

Upvotes: 6

Related Questions