Why do we have to always put a LENGTH in substring? Isn't it has to be if you do not put LENGTH, it will get the rest of the string from the START index?

Hi Jester,


Method substring() returns a new string that is a substring of given string.  String substring() method is used to get the substring of a given string based on the passed indexes. There are two variants of this method. In this guide, we will see how to use this method with the help of examples.

1. When we pass only the starting index:

String substring(int beginIndex)

Returns the substring starting from the specified index i.e beginIndex and extends to the character present at the end of the string. For example – "Chaitanya".substring(2) would return "aitanya". The beginIndex is inclusive, that is why the character present at the index 2 is included in the substring. This method throws IndexOutOfBoundsException If the beginIndex is less than zero or greater than the length of String (beginIndex<0||> length of String).


2. When we pass both the indexes, starting index and end index:

String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. Thus the length of the substring is endIndex-beginIndex. In other words you can say that beginIndex is inclusive and endIndex is exclusive while getting the substring.

For example – "Chaitanya".substring(2,5) would return "ait". It throws IndexOutOfBoundsException If the beginIndex is less than zero OR beginIndex > endIndex OR endIndex is greater than the length of String.


Solution

Hi Jester,


Yes, in Outsystems you always need to provide the length of the substring. This length may exceed the length of the string.

I like the idea of not needing to provide the length when you just want the rest of the string returned. Maybe you can submit this as an idea (https://www.outsystems.com/ideas/)?


@Rahul: My guess is that Jester was talking about the Substr function of Outsystems:

https://success.outsystems.com/Documentation/11/Reference/OutSystems_Language/Logic/Built-in_Functions/Text#Substr


Substr

Returns a sub-string of 't' beginning at 'start' zero-based position and with 'length' characters.

Available in:

  • Server-side logic: Yes
  • Client-side logic: Yes
  • Database: Can be used with attributes in aggregates.
  • Local Storage: Can be used with attributes in aggregates.


Parameters

t
Type: Text. Mandatory.
The Text where the operation is performed.
start
Type: Integer. Mandatory.
The zero-based position to start the Text extraction from.
length
Type: Integer. Mandatory.
The number of characters to include in the output Text.


Solution

@Rahul Sahu thanks for responding.

@Lennart Kraak thanks for responding as well. I'll be raising this as an idea as you mentioned. It just came into my mind because in most of traditional programming language, you don't have to place the length for the SUBSTR when you want to get the rest of the string from the START Index.

It will be a simple adjustment for Outsystems, I guess. Thanks again!