Function: SubString
Function: SubString

Function: SubString

SubString ( STRING, START, END )

Gets a string from a larger string using a provided string index interval.

Parameters

Parameter Type Description
STRING VALUE(STRING) The string to sample from.
START VALUE(INTEGER) The starting index (0-based), inclusive.
END VALUE(INTEGER) The ending index (0-based), exclusive.

Returns

VALUE(STRING) The result of getting a substring from the provided string.

What It Does

This returns a string that is the result of copying the characters from a string from a starting index, inclusively, to an ending index, exclusively (0 to 3 means characters 0, 1, and 2). The input value is interpreted as a string, and the indices are interpreted as integers.

If an index is less than 0, it is clamped to 0, and if an index is greater than the length of the string, it is clamped to the length of the string. If the ending index is less than or equal to the first, the empty string is returned.


	a = substring("applesauce", 0, 5);  // a = "apple"
	a = substring("applesauce", 5, 10); // a = "sauce"
	a = substring("applesauce", 10, 5); // a = "" (5 is less than 10)
	a = substring(12345678, 1, 4);      // a = "234" (12345678 is converted to a string, first)

Example

String Manipulation Examples


world
{
	start()
	{
		// Concatenation
		textln("to" + "gether");                                // "together" 
		textln(strConcat("to", "gether"));                      // "together" 
		textln(strConcat(strConcat("to", "gether"), "ness"));	// "togetherness"
		textln(4 + 5);											// 9
		textln(strConcat(4, 5));								// "45" (both values are converted to strings first)
		textln("");

		// Substring
		textln(substring("applejack", 0, 5));		// "apple" 
		textln(substring("applejack", 5, 9));		// "jack" 
		textln(substring("applejack", 9, 5));		// "" 
		textln(substring("applejack", -3, 100));	// "applejack" = substring("applejack", 0, 9)
		textln(substring(123, 0, 1));				// "1" (first value is converted to a string first)
		textln("");

		// Lower-case convert.
		textln(strLower("Apples and Oranges"));		// "apples and oranges"
		textln(strLower("HELLO!!!"));				// "hello!!!"
		textln(strLower("123456789"));				// "123456789"
		textln("");
		
		// Upper-case convert.
		textln(strUpper("Apples and Oranges"));		// "APPLES AND ORANGES"
		textln(strUpper("hey."));					// "HEY."
		textln(strUpper("123456789"));				// "123456789"
		textln("");

		// Get single character.
		textln(strChar("Apples and Oranges", 0));	// "A"
		textln(strChar("This is a string.", 5));	// "i"
		textln(strChar(12345, 2));					// "3" (converted to string first)
		textln(strChar("apple", -1));				// "" (out of range)
		textln("");

		// string trim.
		textln(strTrim("      apple"));				// "apple"
		textln(strTrim("apple      "));				// "apple"
		textln(strTrim("   apple   "));				// "apple"
		textln(strTrim("\tapple\t"));				// "apple"
						 
		quit;
	}
}
×

Modal Header