Lang.String Class
Namespace: Lang
Represents a string
Since R2026a
Description
The PQL class String represents the strings in your code.
// simple C++ variables containing string literals (tree-sitter node: string) const char *s1 = "hello"; std::string s2 = "example"; auto s3 = "";
The various components in the C++ code can be detected as strings and then the predicates of this classes can be applied on the detected strings.
Predicates
| Type | Raisable | Printable |
|---|---|---|
String
| No | Yes |
This class defines these predicates that act on the objects of this class.
| Predicates | Description | Example |
|---|---|---|
strlen(Lang.String self, required Lang.Int &len)
| Returns the length (number of characters) of self into len. | This PQL defect checks for a variable name string with a specific length. defect CheckLen =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and name.strlen(&n)
and n == 5
raise "length is 5"
on sIn this C++ code, the defect finds a variable with a name that has a length 5.
int main() {
const char *msg = "hello"; // length 5
(void)msg;
return 0;
} |
contains(Lang.String self, Lang.String sub)
| Return true when sub occurs as a substring in the string. | This PQL defect checks for occurrences of a substring inside strings. defect HasTest =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.contains("test")
raise "contains 'test'"
on vIn this C++ code, the defect finds a variable with a name containing "test".
std::string detest = "unit_config"; |
substr(Lang.String self, Lang.Int index, Lang.Int length, required Lang.String &result)
| Extract the substring starting at index of length length (or shorter at bounds) and bind it to result. | This PQL defect checks that a substring of a variable name equals an expected value. defect SubStrCheck =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.substr(0, 4, &r)
and r == "exam"
raise "prefix is 'exam'"
on vIn this C++ code, the defect inspects the first 4 chars of the variable name.
std::string example = "str"; |
match(Lang.String self, Lang.String regex)
| Test whether the whole string matches the given regular expression. | This PQL defect checks whether a variable name matches a regex pattern. defect MatchCaps =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.match("[A-Z][A-Z0-9_]+")
raise "matches all-caps identifier"
on vIn this C++ code, the defect looks for a literal that matches an all-caps identifier pattern.
const char *VAR = "str"; |
counts(Lang.String self, Lang.String sub, required Lang.Int &nb)
| Count the non-overlapping occurrences of sub in the string and bind the count to nb. | This PQL defect checks the number of occurrences of a substring in a variable name. defect CountA =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s == "banana"
and s.counts("a", &n)
and n == 3
raise "three 'a's"
on vIn this C++ code, the defect verifies the count of 'a' in variable name.
const char *banana = "str"; |
endsWith(Lang.String self, Lang.String suffix)
| Return true when the string ends with suffix. | This PQL defect checks filenames or strings that end with a specific suffix. defect =
when
Cpp.Variable.is(&variable)
and variable.filename(&filename)
and filename.endsWith(".cpp")
raise "Filename should not end with '.cpp'"
on variableIn this C++ code, the defect checks the path's filename literal ends with ".cpp".
// example.cpp const char *path ; |
startsWith(Lang.String self, Lang.String prefix)
| Return true when the string starts with prefix. | This PQL defect checks for variable name beginning with a given prefix. defect StartsEx =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.startsWith("ex")
raise "starts with ex"
on vIn this C++ code, the defect finds a variable name that begins with "ex".
std::string example = "str"; |
index(Lang.String self, Lang.String sub, required Lang.Int &idx)
| Return the index of first occurrence of sub (0-based) or -1 if not found, bind to idx. | This PQL defect checks the index of a substring inside a variable name. defect IndexOfA =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.index("a", &i)
and i == 2
raise "index of 'a' is 2"
on vIn this C++ code, the defect inspects the first occurrence index of 'a' in the variable name.
std::string example = "str"; |
lastIndex(Lang.String self, Lang.String sub, required Lang.Int &idx)
| Return the index of the last occurrence of sub (0-based) or -1; bind to idx. | This PQL defect checks the last occurrence index of a substring in a variable name. defect LastIndexE =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.lastIndex("e", &i)
and s == "example"
and i == 6
raise "last index of 'e' is 6"
on vIn this C++ code, the defect verifies the last index of 'e' in the variable name
const char *example = "foo"; |
indexFrom(Lang.String self, Lang.String sub, Lang.Int from, required Lang.Int &idx)
| Find first occurrence of sub at or after from, return index or -1 in idx. | This PQL defect checks substring search starting from a given offset. defect IndexFromA =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s == "foobarbaz"
and s.indexFrom("o", 3, &i)
and i == -1
raise "no 'o' after position 3"
on vIn this C++ code, the defect checks that no 'o' exists in the variable name "foobarbaz" at or after position 3.
std::string foobarbaz = "str"; |
charAt(Lang.String self, Lang.Int idx, required Lang.String &c)
| Return the single-character substring at idx (as string) in c; out-of-range may be empty. | This PQL defect checks a specific character at a specific index in a variable name. defect CharIsE =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.charAt(1, &c)
and c == "e"
raise "char at 1 is 'e'"
on vIn this C++ code, the defect checks the second character of the variable name.
const char *hello = "str"; |
regexSearch(Lang.String self, Lang.String regex)
| Search the string for a substring matching regex; true if any match exists. | This PQL defect checks whether a regex matches any substring of a variable name. defect HasTodo =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.regexSearch("(foo|Foo|FOO)")
raise "contains TODO token"
on vIn this C++ code, the defect finds variable names that uses placeholder names.
const char *foo = "str"; |
regexMatchesCaseInsensitive(Lang.String self, Lang.String regex)
| Case-insensitive full-match of the string against regex. | This PQL defect checks a case-insensitive regex full match. defect CaseInsensitiveHello =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.regexMatchesCaseInsensitive("[hH]ello")
raise "case-insensitive match of hello"
on vIn this C++ code, the defect finds a variable name that matches "hello" ignoring case.
std::string hello = "str"; |
truncate(Lang.String self, Lang.Int length, required Lang.String &res)
| Truncate the string to at most length characters and bind the result to res. | This PQL defect checks that truncation of a variable name yields an expected prefix. defect Trunc =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.truncate(4, &r)
and r == "exam"
raise "truncated to exam"
on vIn this C++ code, the defect checks for variable names with expected prefix
std::string example = "str"; |
trimLeft(Lang.String self, Lang.Int index, required Lang.String &res)
| Return the substring starting at index (trim left) in res, with bounds handling. | This PQL defect checks the trailing part of a variable name. defect TrimLeft =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s == "example"
and s.trimLeft(2, &r)
and r == "ample"
raise "trimLeft produced 'ample'"
on vIn this C++ code, the defect checks trimmed result from position 2.
auto example = "str"; |
containsNonASCIIChar(Lang.String self)
| Return true if the string contains any non-ASCII character (codepoint > 127). | This PQL defect checks for non-ASCII characters variable names. defect NonAscii =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.containsNonASCIIChar()
raise "non-ASCII present"
on v |
isLowerCamelCase(Lang.String self)
| True if the string follows lowerCamelCase (starts with lowercase, internal capital letters, no separators). | This PQL defect checks variables with lower camel-case names in code. defect LowerCamel =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s == "lowerCamelCase"
and s.isLowerCamelCase()
raise "lower camel case"
on vIn this C++ code, the defect matches a variable whose name is in lowerCamelCase.
std::string varname = "str"; |
isUpperCamelCase(Lang.String self)
| True if the string follows UpperCamelCase (starts with uppercase, internal capitals, no separators). | This PQL defect checks for UpperCamelCase variable names. defect UpperCamel =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.isUpperCamelCase()
raise "UpperCamelCase found"
on vIn this C++ code, the defect finds a literal in UpperCamelCase form.
const char *VarName = "str"; |
isLowerSnakeCase(Lang.String self)
| True if the string follows lower_snake_case (lowercase letters and underscores). | This PQL defect checks for lower snake-case variable names. defect LowerSnake =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.isLowerSnakeCase()
raise "lower_snake_case found"
on vIn this C++ code, the defect detects a variable name using lower snake case.
std::string var_name = "str"; |
isUpperSnakeCase(Lang.String self)
| True if the string follows UPPER_SNAKE_CASE (uppercase letters and underscores). | This PQL defect checks for upper snake-case variable names. defect UpperSnake =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s.isUpperSnakeCase()
raise "UPPER_SNAKE_CASE found"
on vIn this C++ code, the defect matches a variable name in upper snake case.
const char *VAR_NAME = "str"; |
pathFilename(Lang.String self, required Lang.String &filename)
| Extract the filename component from a path string and bind it to filename. | This PQL defect extracts the filename from a path literal. defect PathFile =
when
Cpp.Variable.is(&v)
and v.path(&p)
and p.pathFilename(&fn)
and fn == "string.cpp"
raise "filename is string.cpp"
on vIn this C++ code, the defect find the name of the file in which the variable is declared.
// source file located in src/string.cpp const char *var; |
pathExtension(Lang.String self, required Lang.String &extension)
| Return the file extension (including dot) or empty string if none, in extension. | This PQL defect checks the path extension of the source file in which a variable is declared. defect PathExt =
when
Cpp.Variable.is(&v)
and v.path(&p)
and p.pathExtension(&ext)
and ext == ".cpp"
raise "extension is .cpp"
on vIn this C++ code, the defect verifies the path
literal's extension is "
// source file located in src/string.cpp const char *var; |
pathStem(Lang.String self, required Lang.String &stem)
| Return the stem (filename without extension) of a path into stem. | This PQL defect checks the path stem of the source file in which a variable is declared the stem of a path literal. defect PathStem =
when
Cpp.Variable.is(&v)
and v.path(&p)
and p.pathStem(&st)
and st == "string"
raise "stem is string"
on vIn this C++ code, the defect extracts the filename
stem is
// source file located in src/string.cpp const char *var; |
createString(Lang.String in, required Lang.String &out)
| Create a new string value equal to in and bind it to out. | This PQL code demonstrates creating a new string value from an existing literal. defect MakeString =
when
Cpp.Variable.is(&v)
and v.name(&s)
and createString(s, &t)
and t == "hello"
raise "created string 'hello'"
on v |
catString(Lang.String in1, Lang.String in2, required Lang.String &out)
| Concatenate in1 and in2, binding the result to out. | This PQL code shows concatenation of two literal parts. defect Concat =
when
Cpp.Variable.is(&v)
and v.name(&s)
and catString("foo", "bar", &r)
and r == "foobar"
raise "concatenated to foobar"
on v |
replaceAll(Lang.String self, Lang.String from, Lang.String to, required Lang.String &result)
| Replace all occurrences of from with to in the string and bind to result. | This PQL defect checks that a global replacement produces the expected string. defect ReplaceAllExample =
when
Cpp.Variable.is(&v)
and v.name(&s)
and s == "foobarfoo"
and s.replaceAll("foo","baz",&r)
and r == "bazbarbaz"
raise "replaceAll result matched"
on v |
replaceRegexAll(Lang.String self, Lang.String pattern, Lang.String
replacement, required Lang.String &result) | Replace all occurrences of the regular expression pattern
with replacement in the string and returns the result as
result. | This PQL defect checks that a global replacement replacement produces the expected string. defect ReplaceRegexAllExample =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and name == "foo123bar44kkk65LLL000"
and name.replaceRegexAll("[0-9]+", "_", &result)
and result == "foo_bar_kkk_LLL_"
raise "replaceRegexAll: '{name}' with '[0-9]+'->'_' gives '{result}'"
on variable |
replaceFirst(Lang.String self, Lang.String from, Lang.String to,
required Lang.String &result) | Finds the first instance of from in self
and replaces it with to. The result is returned as
result. | This PQL defect checks that a global replacement replacement produces the expected string. defect ReplaceFirstExample =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and name == "foobarfoo"
and name.replaceFirst("foo", "baz", &result)
and result == "bazbarfoo"
raise "replaceFirst: '{name}' with 'foo'->'baz' gives '{result}'"
on variable |
replaceRegexFirst(Lang.String self, Lang.String pattern, Lang.String
replacement, required Lang.String &result) | Finds the first match for the regular expression pattern in
self and replaces it with replacement. The
result is returned as result. |
defect ReplaceRegexFirstExample =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and name == "foo123bar456"
and name.replaceRegexFirst("[0-9]+", "_", &result)
and result == "foo_bar456"
raise "replaceRegexFirst: '{name}' with '[0-9]+'->'_' gives '{result}'"
on variable |
lowerCase(Lang.String self, required Lang.String
&lower) | Converts self to all lower case. |
defect LowerCaseExample =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and (name == "HelloWorld"
or name == "helloWorld")
and name.lowerCase(&result)
and result == helloworld
raise "lowerCase: '{name}' -> '{result}'"
on variable |
upperCase(Lang.String self, required Lang.String
&upper) | Converts self to all upper case. |
defect UpperCaseExample =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and (name == "HelloWorld"
or name == "helloWorld")
and name.upperCase(&result)
and result == "HELLOWORLD"
raise "upperCase: '{name}' -> '{result}'"
on variable |
titleCase(Lang.String self, required Lang.String
&title) | Converts self to title case. |
defect TitleCaseExample =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and name == "tHis_is_atEsT"
and name.titleCase(&result)
and result == "This_Is_Atest"
raise "titleCase: ''{name}'' -> ''{result}''"
on variable |
repeatChar(Lang.String self, Lang.Int n, required Lang.String
&res) | Constructs a string res by repeating self
by n times. |
defect RepeatCharExample =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and name == "x"
and name.repeatChar(5, &result)
and result == "xxxxx"
raise "repeatChar: ''{name}'' repeated 5 times -> ''{result}''"
on variable |
padString(Lang.String self, Lang.Int length, Lang.String padChar,
required Lang.String &result) | Pads the string self by repeating the substring
padchar until result reaches
length. |
defect PadStringExample =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and name == "foo"
and name.padString(5, "x", &result)
and result == "fooxx"
raise "padString: ''{name}'' padded to 5 chars with 'x' -> ''{result}''"
on variable |
isBlank(Lang.String self) | Returns true if a string is blank. |
defect IsBlankExample =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and name == " "
and name.isBlank()
raise "isBlank: {variable} is blank"
on variable |
compare(Lang.String self, Lang.String str, required Lang.Int
&result) | Compares self and str lexicographically.
The integer result is negative if self appears
before str in lexicographical order. It is zero if both character
sequences compare equivalent. It is positive if self appears
after str in lexicographical order |
defect compareExample =
when
Cpp.Variable.is(&variable)
and variable.name(&name)
and name.compare("hard", &comparisonResult2)
and comparisonResult2 > 0
raise "compare: {name} compared to 'hard' is {comparisonResult2}"
on variable |
Version History
Introduced in R2026a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)