In Power Apps. What's the best method to use when creating/concatenating comma-separated values to prevent them from breaking if the user enters a comma in the field that's captured?
Say if I have a function like this:
ClearCollect(
InformationCollection,
ForAll(Split(Left(BrowseGallery1.Selected.'Multiline Information',Len(BrowseGallery1.Selected.'Multiline Information')-1), "|"),
With({_line: Split(Result, ";")},
{
CSerialNumber: Last(FirstN(_line, 1)).Result,
CUnit: Last(FirstN(_line, 2)).Result,
CUnitOD: Last(FirstN(_line, 3)).Result,
CUnitID: Last(FirstN(_line, 4)).Result,
CUnitID2: Last(FirstN(_line, 5)).Result,
CTest: Last(FirstN(_line, 6)).Result
}
)
)
)
that expands a string that is separated by a semicolon ";" and a pipe "|".
The above solution will break if a user enters a text with a semicolon or a pipe.
What's the best method to prevent this?
- Do I choose my separator as a very rarely used special character?
- Or maybe I deny the user from entering a pipe or semicolon in those fields?
- Or ideally, some sort of extra logic that would escape the additional semicolons used in those fields by users.
Solved! Go to Solution.
There are a couple ways to handle it.
Usually when we separate with special characters, then when the information is input and before concat of the text with the special characters, I do a substitute for those special characters.
So, if a person enters a | and I plan to use that character, then I first substitute the | in the text with something else special - like a #@VP (vp in this case referring to vertical pipe) very unlikely that the user would intentionally enter #@VP in the text, so, the | is substituted with #@VP and THEN to pull that back, after the text is split, there is a substitute done for any #@VP in the text to |
That is one way to do it and allow entry without restriction. That would be the other way...restrict the user from entering in the |
I hope this is helpful for you.
There are a couple ways to handle it.
Usually when we separate with special characters, then when the information is input and before concat of the text with the special characters, I do a substitute for those special characters.
So, if a person enters a | and I plan to use that character, then I first substitute the | in the text with something else special - like a #@VP (vp in this case referring to vertical pipe) very unlikely that the user would intentionally enter #@VP in the text, so, the | is substituted with #@VP and THEN to pull that back, after the text is split, there is a substitute done for any #@VP in the text to |
That is one way to do it and allow entry without restriction. That would be the other way...restrict the user from entering in the |
I hope this is helpful for you.
Totally forgot about substitute but is something I've used before in other languages.
Thanks a bunch!
Ended up doing this when saving the string:
Substitute(Substitute(TextInput2_12.Text,";",":"),"|","/")
I don't know your input, but you might want to consider substituting to something besides ":" or "/" only because those seem like something that would commonly be input.
If you're going to do the substitute "back" to the information originally entered later, that might impact the accuracy.
But...glad you got it working!