SharePoint List View Formatting – How to find length of string
Today, I am going to share a problem and a solution which I faced while working on list view formatting. We are going to see how to get the length of string so based on that length I can perform string operation.
In my project, I want to check if length of string is more than 130 then show three dots (…) or else the text is shown as it is. Moreover, knowing length of string you can hide or show certain elements or change color of text etc.
Earlier I thought, “we have length function to find length of string”, but no, length function returns Boolean value whether string is empty or not i.e. either 1 or 0 is returned. Let’s see the following example of length function:
Output: If you see length(@currentField) returned 1.
Here, we have appended pipe (‘|’) to value of [$Body] field to form a temporary string which has pipe at the end and using indexOf function we are checking index of pipe which returns us length of string. Time to check output.
Output: If you see count is 196, i.e. no. of characters in the string.
So as per my case, no. of characters were more than 130, so it should show three dots.
=if (indexOf([$Body]+'|','|') > 130, substring([$Body],0,130)+'...', [$Body])
Let’s see the output:
For Length function : https://github.com/pnp/List-Formatting/issues/209#issuecomment-579806926
For IndexOf function : https://github.com/pnp/List-Formatting/issues/209#issuecomment-631650565
Please Like, Share, Comment.
Thanks for reading.