I have some underscore-delimited strings like abc_def_ghi
. I would like to get a substring out of the string made of one or more delimited substrings, so that if I call:
getUnderscoreSubstring("abc_def_ghi",2)
then I get:
abc_def
This is the C# code I'm using:
public string getUnderscoreSubstring(string fullStr,int substringCount)
{
string[] splitArray = fullStr.Split('_');
if (substringCount>splitArray.Count())
{
return null;
}
else
{
string output = "";
for(int c=0;c<substringCount;c++)
{
output += splitArray[c];
if (c<substringCount-1)
{
output += "_";
}
}
return output;
}
}
I'm wondering if there's a simpler way to rewrite this, possibly using extension methods.
getUnderscoreSubstring("__abc_def_ghi", 2)
, and why? \$\endgroup\$