SharePoint List View Formatting – How to find length of string

Hi Everyone,

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.

Problem:

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:

“txtContent”:”=length(@currentField)”

Output: If you see length(@currentField) returned 1.

Figure 1: Output using length function

Solution:

“txtContent”:”= indexOf([$Body]+'|','|')”

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.

Figure 2: Output using indexOf function

So as per my case, no. of characters were more than 130, so it should show three dots.

Expression:

=if (indexOf([$Body]+'|','|') > 130, substring([$Body],0,130)+'...', [$Body])

Let’s see the output:

Figure 3: Output as I expected

References:

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.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: