PramodChoudhari
PramodChoudhari

Reputation: 2553

how to create String array dyanamically

I have following string s

string -1> Ferocactus_glaugescens__0000_009843_007280.jpg

string -2> Sanseveria_zeylanica_0000_009568_007476.jpg

string -3> Schefflera_arb_Gold_Capella__0000_008329_007482.jpg

i want create string array of count equals to no of "_" in string . eg:

string -1> contains 5 "_" so my string array will like string []stringArray=new string[5];

string -2> contains 4 "_" so my string array will like string []stringArray=new string[4];

string -3> contains 7 "_" so my string array will like string []stringArray=new string[7];

how do i check no of "_" in given string .

I can check it using for loop and i want other simple solution like linq or linq.

Thanks Pramod

Upvotes: 0

Views: 3333

Answers (3)

Vijay Sirigiri
Vijay Sirigiri

Reputation: 4721

Int32 delimiterCount = " Ferocactus_glaugescens__0000_009843_007280.jpg".Split('_').Length;

Upvotes: 0

user284291
user284291

Reputation:

int no_of_string = s.Split('_').Length;
String[] string = new String[no_of_string];

Upvotes: 4

Johann Blais
Johann Blais

Reputation: 9469

String is an IEnumerable of char, you can do something like:

myString.Where(c => c == '_').Count();

Upvotes: 2

Related Questions