substring

概述

本节介绍 Zoho Creator 中的 substring 函数的语法和使用。

描述

substring 函数提取一个字符串的两个指定索引之间的字符(“从”和“至”,不包括“至”本身),并返回新的子字符串。

语法

<string>.substring(<start index>, <end index>)

(或)

mid(string , start index, end index)

// 仅在脚本构建器的自由流程脚本模式下才支持此格式。

substring 函数语法有以下参数。

string - 输入字符串 

start index - 必需。开始提取处的索引。第一个字符为索引 0

end index - 可选。停止提取处的索引。如果省略,则提取字符串的剩余部分

示例

text="Hello world welcome to the universe";

text.substring(6,11); //返回 "world"

text.substring(12); //返回 "welcome to the universe"

mid("Hello world welcome to the universe",6,11); //返回 "world"

mid("Hello world welcome to the universe", 12); //返回 "welcome to the universe"

电话号码验证示例

此示例说明了一种电话号码验证的情况。假设我们有一个 Zoho Creator 表单用于存储用户的电话号码。我们需要以 (xxx) xxx-xxxx 格式存储数字。例如,

  1. 允许按以下格式输入的数字保持不变地传递:
    (123) 456-7890
  2. 将以这些格式提交的数字转换为上述格式:
    (123)456-7890
    123-456-7890

为达到这个目的,添加以下函数

string Format.Format_Phone_Number (string in_phone)
{
if (input.in_phone == null)
{
//电话号码为空,返回空字符串
return "";
}
ph_num = input.in_phone.getAlphaNumeric(); //删除所有空格并仅返回字母数字字符
ph_num = ph_num.removeAllAlpha(); //removes all letters
if (ph_num == "")
{
//电话号码裁减为空字符串,返回空字符串
return "";
}
if (ph_num.length() != 10)
{
//电话号码的长度不等于 10,意味着它不能成为 1234567890 这样的格式
return "error";
}
ph_num_formatted = (((("(" + ph_num.subString(0,3)) + ") ") + ph_num.subString(3,6)) + "-") + ph_num.subString(6); //adds the special characters ( "(", ")" and "-" in the required places
return ph_num_formatted;
}

该函数将任何插入的电话号码格式修改为所需的 (xxx) xxx-xxxx 格式。您可通过将以下脚本添加到所需的表单动作块中来调用此函数:

Phone_Temp = thisapp.Format.Format_Phone_Number (input.Phone_Number);
if ((Phone_Temp == "") || (Phone_Temp == "error"))
{
alert("Please enter a 10 digit phone number");
}
else
{
if (input.Phone_Number != Phone_Temp)
{
input.Phone_Number = Phone_Temp;
}
}

此脚本返回在“Phone_Number”字段中以所需格式输入的电话号码。

备注

  • “Phone_Number”字段是单行字段类型。