SubString

The first position is always 0.


String.substring ( from [,to] )

Extracts parts of a string and returns the extracted parts in a new string.
from - The zero-based index number indicating the beginning of the substring.
to - (Optional) Zero-based index number indicating the end of the substring.
One argument, we get the string starting from the specified index Inclusive until the end of the string.

sText = "abcdef" 
sSubText = sText.substring(2); 'position 2 = 'c' which is included
console.log(sSubText); // cdef

Two arguments, we get the string starting from the first number to the last number Exclusive.

sText = "abcdef" 
sSubText = sText.substring(0, 2); 'position 2 = 'c' which is excluded
console.log(sSubText); // ab

When the 'from' and 'to' are the same, nothing is returned.
Starting at position 1 (2nd character( to position 1 (2nd character, which is excluded).

sSubText = sText.substring(2, 2);         '=""  

When the 'to' is larger than the 'from', then the arguments are automatically swapped around.
Starting at position 1 (2nd character) to position 4 (5th character, which is excluded).

sSubText = sText.substring(1, 4);         '="bcd"  
sSubText = sText.substring(4, 1); '="bcd"


String.slice ( start [, end] )

Extracts parts of a string and returns the extracted parts in a new string.
start -
end - (Optional) The position (up to, but not including) where to end the extraction
One argument, we get the string starting from the specified index Inclusive until the end of the string.

sText = "abcdef" 
sSubText = sText.slice(2); 'position 2 = 'c' which is included
console.log(sSubText); // cdef

Two arguments, we get the string starting from the first number to the last number Exclusive.

sText = "abcdef" 
sSubText = sText.slice(0, 2); 'position 2 = 'c' which is excluded
console.log(sSubText); // ab

String.substr ( start , length )

Extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
This has been deprecated and should not be used

sText.substr(0, 3) 

VBA Migration

The VBA equivalent is [[Left | Right | Mid]]


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext