SharePoint Designer 2013 workflow loop to remove spaces

Learn how you can use a loop in a SharePoint Designer 2013 workflow to remove all spaces from a text string.

ADVERTISEMENTS

Problem

While you can use the Trim String action in a SharePoint Designer 2013 workflow to remove leading and trailing spaces from a text string, there is no specific action to remove spaces that are embedded in a string.

Solution

You can use a SharePoint Designer workflow loop to loop through substrings in a string to remove any spaces that are embedded within the string, and then concatenate the strings with each other to be able to form a string that does not contain any spaces. In the example below, you are going to remove spaces from the string "SharePoint 2013 lists and libraries".

To use a SharePoint Designer workflow loop to remove spaces from a string:

  1. In SharePoint 2013, create a new SharePoint list named MyList that has one column named Title.
  2. In SharePoint Designer 2013, create a new List Workflow that runs on the MyList SharePoint list. Name the workflow RemoveSpacesFromStringWF and leave SharePoint 2013 Workflow selected as the Platform Type.
  3. Create a local variable of type String named TextString1.
  4. Add a Set Workflow Variable action to the SharePoint workflow, and configure it to say:

    Set Variable: TextString1 to SharePoint 2013 lists and  libraries 
    Note that there are two spaces between "and" and "libraries" (to test the removal of double spaces), and that an extra space was appended to the entire string to be able to find the last substring in the string.
  5. Add a Find Substring in String action to the SharePoint workflow and configure it to say:

    then Find   in Variable: TextString1 (Output to Variable: index)
    Here you are just trying to find the first space in the TextString1 variable.
  6. Click to place the cursor below the last action you added, and then select Workflow > Insert > Loop > Loop with Condition, and configure the sentence for the loop to say:

    The contents of this loop will run repeatedly while: Variable: index not equals -1
  7. Click to place the cursor inside the loop, and then add an Extract Substring of String from Index with Length action to the SharePoint workflow, and configure it to say:

    Copy from Variable: TextString1, starting at 0 for Variable: index characters (Output to Variable: substring )
  8. Create a local variable of type String named StringWithoutSpaces.
  9. Click to place the cursor below the last action you added, and then select Workflow > Insert > Condition > If any value equals value, and configure the condition to say:

    If Variable: substring is not empty
  10. Add a Set Workflow Variable action inside the loop, inside of the If-Condition of the SharePoint workflow, and configure it to say:

    Set Variable: StringWithoutSpaces to [%Variable: StringWithoutSpaces%][%Variable: substring%]
    where you must use the String Builder dialog box to concatenate the strings that are stored in the variables.
  11. Add a Do Calculation action inside the loop but outside of the If-Condition of the SharePoint workflow, and configure it to say:

    then Calculate Variable: index plus 1 (Output to Variable: calc)
  12. Add an Extract Substring from Index of String action inside the loop but outside of the If-Condition of the SharePoint workflow, and configure it to say:

    then Copy from Variable: TextString1, starting at Variable: calc (Output to Variable: TextString1)
  13. Add a Find Substring in String action inside the loop but outside of the If-Condition of the SharePoint workflow, and configure it to say:

    then Find   in Variable: TextString1 (Output to Variable: index)
    Here you are trying to find the next space in what is left of the string stored in the TextString1 variable. This is also the last action within the loop. If the value of index is equal to -1, so no space is found, the looping will end; otherwise it will continue looping through the string stored in TextString1 until it does not find any more spaces.
  14. Add a Log to History List action outside the loop to the SharePoint workflow, and configure it to say:

    then Log Variable: StringWithoutSpaces to the workflow history list
  15. End the SharePoint workflow with a Go to a stage action.
  16. Configure the SharePoint workflow to start automatically when an item is created.
  17. Publish the workflow to SharePoint 2013.

In SharePoint 2013, navigate to the MyList SharePoint list and add a new item. Once the workflow has run and completed, navigate to the workflow history list, and verify that the string SharePoint2013listsandlibraries was written to the history list.

Discussion

In the solution described above, you learned how to use a loop in a SharePoint Designer 2013 workflow to loop through a string and remove all spaces from it.

While you set the value of the TextString1 variable to a static string in the solution described above, you could also dynamically set the value of the TextString1 variable by setting it for example to be equal to the value of a field in a SharePoint list item. If you do this, remember to use an extra Set Workflow Variable action that appends an extra space to the TextString1 variable before you start looping through the string. The action should look something like:

then Set Variable: TextString1 to [%Variable: TextString1%] 

where you must again make use of the String Builder dialog box to concatenate a character, which in this case is a space, to the TextString1 variable.

Related Articles

ADVERTISEMENTS