rspec/rules/S4635/description.adoc

13 lines
831 B
Plaintext
Raw Normal View History

2020-12-23 14:59:06 +01:00
Looking for a given substring starting from a specified offset can be achieved by such code: ``str.substring(beginIndex).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
2020-12-23 14:59:06 +01:00
To avoid performance problems, ``String.substring(beginIndex)`` should not be chained with the following methods:
2020-12-23 14:59:06 +01:00
* ``indexOf(int ch)``
* ``indexOf(String str)``
* ``lastIndexOf(int ch)``
* ``lastIndexOf(String str)``
* ``startsWith(String prefix)``
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.
2020-12-23 14:59:06 +01:00
Using these methods gives the same result while avoiding the creation of additional ``String`` instances.