Function: StrChar
Function: StrChar

Function: StrChar

StrChar ( STRING, INDEX )

Returns the character at a specific index in a string.

Parameters

Parameter Type Description
STRING VALUE(STRING) The input string.
INDEX VALUE(INTEGER) The 0-based index into the string.

Returns

VALUE(STRING) A new string containing the character at the provided index.

What It Does

This returns a string containing a single character: the character found at a specific index in a string. If the index is less than 0 or greater than or equal to the length of the provided string, then an empty string is returned. The string parameter is always interpreted as a string, and the index is always converted to/parsed as an integer.


	a = strChar("apple", 2);     // a = "p"
	a = strChar("apple", 4);     // a = "e"
	a = strChar("apple", -1);    // a = ""
	a = strChar("apple", -10);   // a = ""

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