0

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

3 Answers 3

4
int no_of_string = s.Split('_').Length;
String[] string = new String[no_of_string];
Sign up to request clarification or add additional context in comments.

3 Comments

Good solution but the simple arrays use "Length" property for array size instead of count method.
@Dubas : Really thanks ! This is mix up with javaScript I am working on write now.
@infoSetu : there is no count() method on an array ;) int nb = s.Split('_').GetLength(0) - 1;
2

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

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.