web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Power Melange / Difference between Slice, S...

Difference between Slice, Substring & Split

PowerMelangeByShalinee Profile Picture PowerMelangeByShalinee 70

Today we are going to read about the 3 String Functions – Slice, SubString & Split.

PS: As the name suggests, String functions work only on strings.

  1. SLICE: This returns a substring by specifying the starting and ending position of the string inclusive of the Starting Index but not including the ending one.

slice(‘<text>’, <startIndex>, <endIndex>)

Example:

In the compose expression, i have written [slice(variables(‘String’),2,5)]

Thus the result here is: ali

Some interesting facts here would be:

  • If startIndex is greater than the string length, it returns an empty string.
  • If endIndex isn’t specified or greater than the string length, it takes up to the end of the string.
  • If startIndex or endIndex is negative, it calculates the index value as the sum of the string length and the Index and then calculates accordingly. Bet you did not know that.

Lets see some examples by using the following String!
S – 0
H – 1
A -2
L -3
I -4
N -5
E -6
E -7

Length of String: 8

slice(‘Shalinee’, 3) // Returns ‘linee’.
slice(‘Shalinee’, 30) // Returns ”.
slice(‘Shalinee’, 10, 2) // Returns ”.
slice(‘Shalinee’, 0) // Returns ‘Shalinee’.
slice(‘Shalinee’, 2, 5) // Returns ‘ali’.
slice(‘Shalinee’, 6, 20) // Returns ‘ee’.
slice(‘Shalinee’, -2) // Returns ‘ee’. [-2+8 = 6]
slice(‘Shalinee’, 3, -2) // Returns ‘lin’. [-2+8 =6 , thus 3,6)
slice(‘Shalinee’, 3, 3) // Returns an empty string -”.

2. SUBSTRING: This returns a substring starting from the specified index and goes till the number of characters that you want in the substring.

substring(‘<text>’, <startIndex>, <length>)

Example: substring(‘Shalinee’, 5, 3) gives nee

Some interesting facts here would be:

  • The start index should be a greater than or equal to 0 & cannot be negative as in the case of slice. Also it should be less than the length of the string otherwise you will get errors.
  • The length parameter is a positive number & is optional and if not provided, you will get an error.

3. SPLIT: This returns an array that contains substrings, separated by commas, based on the specified delimiter character in the original string.

split(‘<text>’, ‘<delimiter>’)

Example: split(‘Sha_li_nee’, ‘_’) gives [“Sha”,”li”,”nee”]

Some interesting facts here would be:

  • If no delimiter is provided, it gives an error.
  • If a delimiter is provided which is not present in the String, or if nothing is provided within the string, it gives the original string in an array form.

split(‘Sha_li_nee’, ”) gives [“Shalinee”]

Hope it helps!

& the Power Quote of the day is:

“Keep your face always toward the sunshine, and shadows will fall behind you.”


This was originally posted here.

Comments

*This post is locked for comments