I have code that relies heavily on the String.Substring method, to the point where the Substring method is slowing down my application. I know that the range I want to retrieve is within the String (It is not out of bounds.) Is there something that I could use instead of Substring that would be faster? Could I write my own Substring method that could forgo any bounds checks?
sample code:
public String get_element(int element_number) {
int count = 0;
int start_index = 0;
int end_index = 0;
int current_index = 0;
while (count < element_number && current_index != -1) {
current_index = line_text.IndexOf(x12_reader.element_delimiter, start_index);
start_index = current_index + 1;
count++;
}
if (current_index != -1) {
end_index = line_text.IndexOf(x12_reader.element_delimiter, start_index);
if (end_index == -1) end_index = line_text.Length;
return line_text.Substring(start_index, end_index - start_index); ;
} else {
return "";
}
}
I see lots of comments asking if Substring is really the problem. I know that Substring is the problem. I have run profiling in Visual Studio and It has pointed to Substring as the culprit. Also I cannot call this function any less than I currently am. The only place I have left to optimize is the Substring function. I know this is the case.