splice


Description:

public string splice (long start, long end, string? str = null)

Removes a portion of the string and replaces it with something else.

Removes the characters designated by start and end from the string, and replaces them with str, if supplied.

If start or end is negative, string.splice uses it as a character index from the end of the string.

Example: Remove a portion of the string and replaces it:

static int main (string[] args) {
string wisdom = "水は方円の器に従い、人は善悪の友による。";
int start = wisdom.index_of_nth_char (5); // 器
int end = wisdom.index_of_nth_char (10); // 人

// Output:
// ``水は方円の器に従い、人は善悪の友による。``
// ``水は方円の人は善悪の友による。``
// ``水は方円のREPLACEMENT人は善悪の友による。``
string res1 = wisdom.splice (start, end);
string res2 = wisdom.splice (start, end, "REPLACEMENT");
print ("%s\n", wisdom);
print ("%s\n", res1);
print ("%s\n", res2);

return 0;
}

valac --pkg glib-2.0 string.splice.vala

Parameters:

start

the zero-based byte-index at which to begin extraction.

end

the zero-based byte-index at which to end extraction.

str

removed chars are replaced with this string.

Returns:

a newly-allocated string holding the result.