2021-01-27 13:42:22 +01:00
Looking for a given substring starting from a specified offset can be achieved by such code: ``++str.Substring(startIndex).IndexOf(char1)++``. This works well, but it creates a new ``++string++`` for each call to the ``++Substring++`` method. When this is done in a loop, a lot of ``++strings++`` are created for nothing, which can lead to performance problems if ``++str++`` is large.
2020-06-30 12:49:37 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
To avoid performance problems, ``++string.Substring(startIndex)++`` should not be chained with the following methods:
2021-01-06 17:38:34 +01:00
2021-01-27 13:42:22 +01:00
* ``++IndexOf++``
* ``++IndexOfAny++``
* ``++LastIndexOf++``
* ``++LastIndexOfAny++``
2020-06-30 12:49:37 +02:00
For each of these methods, another method with an additional parameter is available to specify an offset.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
Using these methods gives the same result while avoiding the creation of additional ``++String++`` instances.
2020-06-30 12:49:37 +02:00
== Noncompliant Code Example
----
str.Substring(StartIndex).IndexOf(char1); // Noncompliant; a new string is going to be created by "Substring"
----
== Compliant Solution
----
2021-01-04 15:48:41 +01:00
str.IndexOf(char1, startIndex) - startIndex;
2020-06-30 12:49:37 +02:00
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]