Some methods for the string objects that use Regular Expressions are described elsewhere in the file manual (go to String Object Methods that Use Regular Expression Objects for more information). The remainder are described in the links below.
Methods Returning a Character or a Character Code at a Specific Index
strObj.charAt(index)charAt returns the character at the specified index. Valid values for index are between 0 and the length of the string minus 1. charAt with an index out of valid range returns an empty string.
strObj.charCodeAt(index)charCodeAt returns an integer representing the Unicode encoding of the character at the specified index. index is a number between 0 and the length of the string minus 1. If there is no character at the specified index, NaN is returned.
Building a String from a Number of Unicode Characters
String.fromCharCode({code1{, code2{, ...{, codeN}}}})
fromCharCode returns a string from a number of Unicode character values. If no arguments are supplied, the result is the empty string.
A String object need not be created before calling fromCharCode. The method can be applied directly on the object type.
In the following example, txt is set to the string "Confirmit":
var txt = String.fromCharCode(67,111,110,102,105,114,109,105,116);This is a way to be able to set Unicode text in scripts.
Changing Case
strObj.toLowerCase()
toLowerCase returns a string where all alphabetic characters have been converted to lowercase.
strObj.toUpperCase()
toUpperCase returns a string where all alphabetic characters have been converted to uppercase.
Both of these methods have no effect on non-alphabetic characters.
Checking a User Name and Password where Username is Case Insensitive
You can password-protect an open survey with a password and user name combination that is the same for all respondents. This can be used when you do not upload any respondent list before starting the survey, but still want to limit the access to the survey. You should be aware that this would not stop the respondents from accessing the survey more than once (go to Validation Code for more information).
The user name and password can be given in two open text questions, username and password, the latter with the password property. If you want the password to be case sensitive, but not the username you can use a validation code like this:
if(f("username").get().toUpperCase() != "USERNAME" || f("password").get() != "Password")
{
RaiseError();
SetQuestionErrorMessage(LangIDs.en,"Wrong username or password. Please try again.");
}Searching for a Substring within a String
strObj.indexOf(subString{, startIndex})indexOf returns the character position of the first occurrence of a string subString within a String object. startIndex is optional, and is an integer value specifying the index to begin the search. If omitted, searching starts at the beginning of the string. If the subString is not found, -1 is returned.
If startIndex is negative, startIndex is treated as zero. If it is larger than the greatest character position index, it is treated as the largest possible index.
Searching is performed from left to right.
strObj.lastIndexOf(substring{, startindex})
returns the character position of the last occurrence of a subString within a String object. startIndex is optional, and is an integer value specifying the index to begin searching within the String object. If omitted, searching begins at the end of the string. If the subString is not found, a-1 is returned.
If startIndex is negative, startIndex is treated as zero. If it is larger than the greatest character position index, it is treated as the largest possible index.
Searching is performed right to left.
On the Fly Recoding
You may use the indexOf method to search for strings within a text. This can be done to simplify the coding. For example if you have an open text question about car brands you can search for strings like VOLVO or FORD as below:
if(f("openbrands").get().toUpperCase().indexOf("FORD") != -1)
{
f("brands")["1"].set("1");
}
if(f("openbrands").get().toUpperCase().indexOf("MERCEDES") != -1)
{
f("brands")["2"].set("1");
}
if(f("openbrands").get().toUpperCase().indexOf("VOLVO") != -1)
{
f("brands")["3"].set("1");
}and so on. (openbrands is here the open text question, and brands is a hidden multi question used e.g. for reporting).
Here we use toUpperCase to convert case to make the search case insensitive, so that the strings "Volvo", "volvo" and "VOLVO" all will be recognized.
If indexOf returns anything different from –1, it means that the string has been found in the openbrands question.
The problem with this solution is respondents who spell brand names wrongly. You can of course check for different common misspellings ("VOVLO" etc.) but usually you will need some form of manual coding as well.
Retrieving a Section of a String Substring
stringObj.slice(start, {end})returns a section of a string. start is required and is the index of the first character in the section of stringObj. end is optional and is the index after the last character in the section of stringObj. The slice method copies up to, but not including, the element indicated by end.
In this example:
var txt = "The methods of the String Object can be used for text manipulation.";
var section = txt.slice(19,32);
section will be set to the substring
"String Object"If start is negative, it is treated as length+start where length is the length of the string. If end is negative, it is treated as length+end where length is the length of the string. If end is omitted, extraction continues to the end of stringObj. If end occurs before start, no characters are copied to the new string.
stingObj.substring(start, end)
returns the substring at the specified location within a String object.
start is the index indicating the beginning of the substring and end is the index indicating the end of the substring. The substring method returns a string containing the substring from start up to, but not including, end.
The substring method uses the lower value of start and end as the beginning point of the substring. For example, stringObj.substring(0,3) and stringObj.substring(3,0) return the same substring.
If either start or end is NaN or negative, it is replaced with zero.
The length of the substring is equal to the absolute value of the difference between start and end. For example, the length of the substring returned in stringObj.substring(0,3) and stringObj.substring(3,0) is three.
Additionally, in the JScript engine only, there is the function substr():
JScript example:
stringObj.substr(start {, length })
returns a substring beginning at a specified location start and having a specified length.
start is required, and is the starting position (index) of the desired substring. length is optional and is the number of characters to include in the returned substring.
If length is zero or negative, an empty string is returned. If not specified, the substring continues to the end of the string.
Note: substr() is fully deprecated in the JavaScript engine. You will need to use the substring() method in its place.
stringObj.substring(start, end)
returns the substring at the specified location within a String object.
start is the index indicating the beginning of the substring and end is the index indicating the end of the substring. The substring method returns a string containing the substring from start up to, but not including, end.
The substring method uses the lower value of start and end as the beginning point of the substring. For example, stringObj.substring(0,3) and stringObj.substring(3,0) return the same substring.
If either startor end is NaN or negative, it is replaced with zero.
The length of the substring is equal to the absolute value of the difference between start and end. For example, the length of the substring returned in stringObj.substring(0,3) and strObj.substring(3,0) is three.
Replacing Last Comma with "and" in a Listing of Answers
If you refer to a multi question, e.g. brands, in response piping with ^'s in a question text, then the last two items in the listing are separated with "and" in English.
^f("brands")^If the brands "Ford","Mercedes","Volvo" are answers to the brands question, the string returned will be "Ford, Mercedes and Volvo".
However, when you refer to
f("brands").categoryLabels()in a script node, e.g. to include the answers in an email text, the result will be an array with the elements. When this is converted to a string (for example by adding it to a string expression), you get a string that lists the elements separated with commas, but not with "and" between the last two elements: "Ford,Mercedes,Volvo".
The following script will replace the last comma with " and ". (Observe the spaces in front of and after and).
var body : String = "";
body += "Here are the answers on the brands question:\n\n"
var form = f("brands");
body += form.categoryLabels();
if(form.size() > 1)
{
body = body.substring(0,body.lastIndexOf(",")) + " and " + body.substring(body.lastIndexOf(",")+1,body.length);
}
SendMail("interviewer@confirmit.com",f("email"),"Answers",body);
If there are two answers or more, the answers string will be set to the substring from the beginning of the answers string to (but excluding) the last comma, the string " and " and the substring from the character after the last comma to the end of the answers string.
Splitting and Joining Strings
stringObj.split({separator{, limit})split returns the array of strings that results when a string is separated into substrings. separator is a string or an instance of a Regular Expression object (go to Regular Expression Object Overview for more information) identifying one or more characters to use in separating the string. If omitted, a single-element array containing the entire string is returned. limit is a value used to limit the number of elements returned in the array. The result of the split method is an array of strings split at each point where separator occurs in stringObj. stringObj itself is not modified. The separator is not returned as part of any array element.
string
1.concat({string2{, string3{, . . . {, stringN}}}})concat returns a string value containing the concatenation of two or more supplied strings. The result of the concat method is equivalent to:
string
1 + string2 + string3 + ... + stringN.
Generating an Array from a String with Values
Let us say you send in information of what products the respondent uses with the url to an open survey, for example like this:
http://survey.confirmit.com/wix/pXXXXXXXX.aspx?products=1x15x17x19
http://survey.confirmit.com/wix/pXXXXXXXX.aspx?products=6x11x20
1, 6, 11, 15, 17, 19 and 20 are different product codes, and the respondent can have any number of these. Sending them in like this gives more condensed urls than sending in one value (yes/no) for each product. It could possibly be a very long list of products.
In the Forsta Plus survey we capture the products list with Request(go to Request for more information), and want to set a hidden multi question products with the values sent in with the url. The products multi question uses codes that are equal to the product codes that are sent in with the url.
To be able to set the products question, we have to split the string returned from Request("products") with "x" as delimiter to get an array with the product codes sent in:
var txt : String = Request("products")+"";
var productArray = txt.split("x");
f("products").set(productArray);
Retrieving the String Value
strObj.toString()
strObj.valueOf()
Both toString and valueOf returns the string value of the String object.
Methods that Add HTML Tags to a String
Note: All of these methods that add HTML tags are fully deprecated in JavaScript, and only available in the JScript engine. Attempting to use any of these methods with the JavaScript engine will cause issues.
There are a number of methods available that adds HTML code to your strings. They are listed in the table below. They may for example be used in your error messages, or in expressions that will be displayed in info or question titles, texts or answers (with response piping).
Method |
Equivalent to |
strVariable.anchor(anchorString) |
strVariable = '<A NAME="'+ anchorString + '">' + strVariable + '</A>' |
strVariable.big() |
strVariable = '<BIG>' + strVariable + '</BIG>' |
strVariable.blink() |
strVariable = '<BLINK>' + strVariable + ' </BLINK>' |
strVariable.bold() |
strVariable = '<B>' + strVariable + '</B>' |
strVariable.fixed() |
strVariable = '<TT>' + strVariable + '</TT>' |
strVariable.fontcolor(colorVal) |
strVariable = '<FONT COLOR="' + colorVal + '">' + strVariable + '</FONT>' |
strVariable.fontsize(intSize) |
strVariable = '<FONT SIZE="' + intSize + '">' + strVariable + '</FONT>' |
strVariable.italics() |
strVariable = '<I>' + strVariable + '</I>' |
strVariable.link(linkstring) |
strVariable = '<A HREF="' + linkstring + '">' + strVariable + '</A>' |
strVariable.small() |
strVariable = '<SMALL>' + strVariable + '</SMALL>' |
strVariable.strike() |
strVariable = '<STRIKE>' + strVariable + '</STRIKE>' |
strVariable.sub() |
strVariable = '<SUB>' + strVariable + '</SUB>' |
strVariable.sup() |
strVariable = '<SUP>' + strVariable + '</SUP>' |
Removing Leading/Trailing White Space Characters
JScript example:
strObj.Trim()
JavaScript example:
strObj.trim()
These methods return a string with all leading/trailing white-space characters removed from the String object. Note that it returns a new string object rather than modifying the existing object. For example in order to remove the leading/trailing spaces from a respondent’s Open Text answer the following can be used:
JScript example:
f(qid).set(f(qid).get().Trim())
JavaScript example:
f(qid).set(f(qid).get().trim())