Q:
I want to get sub strings according to some sign like - .
EX:
if i have string like this :
saturday-sa-0-
and i wanna to get:
saturday
sa
0
I search and find the following method:
string substring = name.Split('-')[i];
my code block sample:
foreach (string name in q)
{
for (int i = 0; i < 3; i++)
{
string substring = name.Split('-')[i];
}
}
but i read the comments about the performance drawbacks when i have a long string ..
my question is: Is there any way to substring according to specific sign and not affect badly on the performance code?
First, you should execute the Split operation only once. I.e., instead of
use
Second, don’t worry about the performance of Strint.Split too much unless
For example, if you have some database operation that takes 1 second, it does not really matter if the subsequent Split operation takes 0.001 or 0.002 seconds.
EDIT: Regarding the code in your comment: You can refactor
to