If you want to count the number of a specific character in a
string in a field or textbox without coding, you can try this out of the box rules of InfoPath...
First, create two fields: Field 1 and Field 2. Field 1 will
contain the text or string that you want to check and Field 2 will contain the number or
result of the count. In our example, we will use the semicolon (;) character
and count the number of instance in Field 1.
Example:
Field 1: Sample1;Sample2;Sample3;Sample4
Field 2: ?
In our example, Field 2 should show 3 as the result.
Next step is to select Field 2 and go to Text Box Properties and select fx button of Default Value.
Enter this formula:
string-length(translate(field1,
"1234567890ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstuvwxyz",
""))
In this formula, we have 2 functions. The first one is
translate.
translate(field1, "1234567890ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstuvwxyz",
"")
We are translating all letters to none or blank without
spaces. Then after translating, we will now count the length of the string
using string-length.
string-length(translate(field1,
"1234567890ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstuvwxyz",
""))
Since we converted all texts and/or characters other than
the semicolon to blank without spaces, what will be left and counted are the semicolons.
Final result will be:
NOTE:
You can add other characters in our original formula. In our
example, we just converted all the texts and no other special characters are
involved. This also works for text box and rich text box.
Intuitive, thanks.
ReplyDeleteWhat if I want it to count the characters and not the words?
ReplyDeleteCan you clarify? In the guide above, we are counting the character semicolon (;).
Delete