1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
String
======
Routines for strings. The String object is not supposed to be used directly (generally speaking). The functions below are available for primitive values of the string type.
Example:
```
// Useful string routines
x = "SurgeScript".toLowerCase(); // "surgescript"
y = x.substr(0, 5); // "surge"
z = x[0]; // "s" (first character of x)
n = y.length; // 5
```
Please note that strings in SurgeScript are immutable. Once a string is set, its individual characters cannot be changed. If you need to modify the content of a string, reassign the variable to a new string.
Properties
----------
#### length
`length`: number, read-only.
The length of the string.
Functions
---------
#### valueOf
`valueOf()`
The primitive value of the string, i.e., the string itself.
*Returns*
The string.
#### toString
`toString()`
Convert to string.
*Returns*
The string itself.
#### equals
`equals(str)`
Compares the string to another string `str`.
*Arguments*
* `str`: string.
*Returns*
Returns `true` if the strings are equal.
#### get
`get(i)`
Gets the `i`-th character of the string. The `[ ]` operator can be used equivalently.
*Arguments*
* `i`: integer number. A value between 0 (inclusive) and the length of the string (exclusive).
*Returns*
The `i`-th character of the string (0-based index).
#### indexOf
`indexOf(str)`
Finds the position of the first occurrence of `str` in the string.
*Arguments*
* `str`: string. The string to be searched for.
*Returns*
The position (0-based index) of the first occurrence of `str` in the string, or *-1* if there is no such occurrence.
*Example*
```
name = "SurgeScript";
a = name.indexOf("Surge"); // a is 0
b = name.indexOf("Neon"); // b is -1
c = name.indexOf("e"); // c is 4
d = name.indexOf("script"); // d is -1, as the search is case-sensitive
```
#### substr
`substr(start, length)`
Extracts the substring starting at position `start` with length `length`.
*Arguments*
* `start`: number. The start position.
* `length`: number. The length of the substring.
*Returns*
The substring with length `length` starting at `start`.
*Example*
```
name = "SurgeScript";
surge = name.substr(0, 5); // "Surge"
script = name.substr(5, 6); // "Script"
e = name.substr(4, 2); // "e"
empty = name.substr(555, 1); // ""
```
#### concat
`concat(str)`
Concatenates two strings. This is the same as using the `+` operator.
*Arguments*
* `str`: string.
*Returns*
The caller string concatenated with `str` at the end.
*Example*
```
name = "Surge".concat("Script"); // SurgeScript
name = "Surge" + "Script"; // SurgeScript
```
#### replace
`replace(oldstr, newstr)`
Replaces all occurrences of `oldstr` to `newstr` in the caller string.
*Arguments*
* `oldstr`: string. The substring to be replaced.
* `newstr`: string. The substring that should appear in the result.
*Returns*
The caller string having all its occurrences of `oldstr` replaced to `newstr`.
*Example*
```
// dst is "Gimacian, Neon and Charge"
src = "Surge, Neon and Charge";
dst = src.replace("Surge", "Gimacian");
```
#### toLowerCase
`toLowerCase()`
Converts the string to lower case.
*Returns*
The string converted to lower case.
#### toUpperCase
`toUpperCase()`
Converts the string to upper case.
*Returns*
The string converted to upper case.
#### isNullOrEmpty
`isNullOrEmpty(value)`
This method of the `String` object can be used directly. It checks if the given `value` is either `null` or an empty string.
*Available since:* SurgeScript 0.5.3
*Arguments*
* `value`: string | `null`. The value to be tested.
*Returns*
Returns `true` if `value` is either `null` or an empty string.
*Example*
```
name = "Surge";
//name = "";
//name = null;
if(!String.isNullOrEmpty(name))
Console.print(name);
```
|