In this article, we discuss useful utilities that you can use to convert, check and manipulate common data types in SplashKit programs.
Written by: Richard Denton and others
Last updated: October 2024
SplashKit’s Utilities library provides a range of useful functions that can assist you with converting, checking, and manipulating common data types in your SplashKit program.
These functions are useful in many areas of programming.
Converting a string to a number
SplashKit provides two useful functions for handling the conversion of a string
to either an int
or a double
.
// Convert the string "2017" to an integer 2017
string some_string = " 2017 " ;
int some_number = convert_to_integer (some_string);
write ( " The value of some_number is: " );
// Convert the string "3.14159265358979" to a double ~3.141593
string pi_string = " 3.14159265358979 " ;
double pi = convert_to_double (pi_string);
write ( " The value of pi is: " );
using static SplashKitSDK . SplashKit;
//Convert the string "2017" to an integer 2017
string some_string = " 2017 " ;
int some_number = ConvertToInteger (some_string);
Write ( " The value of some_number is: " );
//Convert the string "3.14159265358979" to a double ~3.141593
string pi_string = " 3.14159265358979 " ;
double pi = ConvertToDouble (pi_string);
Write ( " The value of pi is: " );
namespace UsefulUtilities
public static void Main ()
//Convert the string "2017" to an integer 2017
string some_string = " 2017 " ;
int some_number = SplashKit . ConvertToInteger (some_string);
SplashKit . Write ( " The value of some_number is: " );
SplashKit . WriteLine (some_number);
//Convert the string "3.14159265358979" to a double ~3.141593
string pi_string = " 3.14159265358979 " ;
double pi = SplashKit . ConvertToDouble (pi_string);
SplashKit . Write ( " The value of pi is: " );
## Convert the string "2017" to an integer 2017
some_number = convert_to_integer ( some_string )
write ( " The value of some_number is: " )
write_line_int ( some_number )
## Convert the string "3.14159265358979" to a double ~3.141593
pi_string = " 3.14159265358979 "
pi = convert_to_double ( pi_string )
write ( " The value of pi is: " )
Consider you want to write a program that accepts two numbers as input from a user, multiplies them, and outputs the result. One way to accomplish this is to use SplashKit’s Convert To Integer function.
// User input will initially be stored in these two strings.
// And then it will be converted and stored in these integers.
int inum_1, inum_2, result;
write ( " Enter first number: " );
write ( " Enter second number: " );
// Convert user input to integers
inum_1 = convert_to_integer (snum_1);
inum_2 = convert_to_integer (snum_2);
result = inum_1 * inum_2;
write (snum_1 + " multiplied by " + snum_2 + " equals " );
The same can be achieved for decimal numbers, simply by swapping convert_to_integer
with convert_to_double
, and using the appropriate data types.
//Don't use integers, instead use doubles
double inum_1, inum_2, result;
inum_1 = convert_to_double (snum_1);
inum_2 = convert_to_double (snum_2);
using static SplashKitSDK . SplashKit;
//User input will initially be stored in these two strings.
// And then it will be converted and stored in these integers.
int inum_1, inum_2, result;
Write ( " Enter first number: " );
Write ( " Enter second number: " );
//Convert user input to integers
inum_1 = ConvertToInteger (snum_1);
inum_2 = ConvertToInteger (snum_2);
result = inum_1 * inum_2;
Write (snum_1 + " multiplied by " + snum_2 + " equals " );
The same can be achieved for decimal numbers, simply by swapping ConvertToInteger
with ConvertToDouble
, and using the appropriate data types.
// Don't use integers, instead use doubles
double inum_1, inum_2, result;
inum_1 = ConvertToDouble (snum_1);
inum_2 = ConvertToDouble (snum_2);
namespace UsefulUtilities
public static void Main ()
//User input will initially be stored in these two strings.
// And then it will be converted and stored in these integers.
int inum_1, inum_2, result;
SplashKit . Write ( " Enter first number: " );
snum_1 = SplashKit . ReadLine ();
SplashKit . Write ( " Enter second number: " );
snum_2 = SplashKit . ReadLine ();
//Convert user input to integers
inum_1 = SplashKit . ConvertToInteger (snum_1);
inum_2 = SplashKit . ConvertToInteger (snum_2);
result = inum_1 * inum_2;
SplashKit . Write (snum_1 + " multiplied by " + snum_2 + " equals " );
SplashKit . WriteLine (result);
The same can be achieved for decimal numbers, simply by swapping SplashKit.ConvertToInteger
with SplashKit.ConvertToDouble
, and using the appropriate data types.
// Don't use integers, instead use doubles
double inum_1, inum_2, result;
inum_1 = SplashKit . ConvertToDouble (snum_1);
inum_2 = SplashKit . ConvertToDouble (snum_2);
write ( " Enter first number: " )
write ( " Enter second number: " )
# Convert user inputs to integers
inum_1 = convert_to_integer ( snum_1 )
inum_2 = convert_to_integer ( snum_2 )
write ( snum_1 + " multiplied by " + snum_2 + " equals " )
The same can be achieved for decimal numbers, simply by swapping convert_to_integer
with convert_to_double
, and using the appropriate data types.
# Don't uses integers, instead use doubles
inum_1 = convert_to_double ( snum_1 )
inum_2 = convert_to_double ( snum_2 )
Checking if a string is a number
Another helpful set of functions provided by SplashKit’s utilities library is Is Integer and Is Number .
Consider the following.
string message_1 = " 9781273 " ;
string message_2 = " 23129739.13 " ;
string message_3 = " Hello world. " ;
if ( is_integer (message_1))
write_line ( " Message 1 contains an integer! " );
if ( is_number (message_1))
write_line ( " Message 1 contains a number! " );
if ( not is_integer (message_2))
write_line ( " Message 2 is not an integer! " );
if ( is_number (message_2))
write_line ( " Message 2 contains a number! " );
if ( not is_integer (message_3))
write_line ( " Message 3 is not an integer! " );
if ( not is_number (message_3))
write_line ( " Message 3 is not a number! " );
using static SplashKitSDK . SplashKit;
string message_1 = " 9781273 " ;
string message_2 = " 23129739.13 " ;
string message_3 = " Hello world. " ;
if ( IsInteger (message_1))
WriteLine ( " Message 1 contains an integer! " );
WriteLine ( " Message 1 contains a number! " );
if ( ! IsInteger (message_2))
WriteLine ( " Message 2 is not an integer! " );
WriteLine ( " Message 2 contains a number! " );
if ( ! IsInteger (message_3))
WriteLine ( " Message 3 is not an integer! " );
if ( ! IsNumber (message_3))
WriteLine ( " Message 3 is not a number! " );
namespace UsefulUtilities
public static void Main ()
string message_1 = " 9781273 " ;
string message_2 = " 23129739.13 " ;
string message_3 = " Hello world. " ;
if ( SplashKit . IsInteger (message_1))
SplashKit . WriteLine ( " Message 1 contains an integer! " );
if ( SplashKit . IsNumber (message_1))
SplashKit . WriteLine ( " Message 1 contains a number! " );
if ( ! SplashKit . IsInteger (message_2))
SplashKit . WriteLine ( " Message 2 is not an integer! " );
if ( SplashKit . IsNumber (message_2))
SplashKit . WriteLine ( " Message 2 contains a number! " );
if ( ! SplashKit . IsInteger (message_3))
SplashKit . WriteLine ( " Message 3 is not an integer! " );
if ( ! SplashKit . IsNumber (message_3))
SplashKit . WriteLine ( " Message 3 is not a number! " );
message_2 = " 23129739.13 "
message_3 = " Hello world. "
if is_integer ( message_1 ):
write_line ( " Message 1 contains an integer! " )
write_line ( " Message 1 contains a number! " )
if not is_integer ( message_2 ):
write_line ( " Message 2 is not an integer! " )
write_line ( " Message 2 contains a number! " )
if not is_integer ( message_3 ):
write_line ( " Message 3 is not an integer! " )
if not is_number ( message_3 ):
write_line ( " Message 3 is not a number! " )
The terminal output should be:
Message 1 contains an integer!
Message 1 contains a number!
Message 2 is not an integer!
Message 2 contains a number!
Message 3 is not an integer!
Message 3 is not a number!
SplashKit’s Is Integer and Is Number functions can be used to validate user input, ensuring the user always enters a valid data type.
* Reads input from a user, only allowing whole numbers.
* @prompt string - The string to display to the user.
int read_integer (string prompt )
// Prompt the user with the message
// Read the user input as a string.
string line = read_line ();
// Check if user input is a valid whole number, loop until it is.
while ( ! is_integer (line))
write_line ( " Please enter a whole number. " );
// Convert the user input to an integer before returning it.
return convert_to_integer (line);
height = read_integer ( " Enter your height in centimetres: " );
using static SplashKitSDK . SplashKit;
static int ReadInteger ( string prompt)
// Prompt the user with the message
// Read the user input as a string.
string line = ReadLine ();
// Loop while the user's input is NOT a valid whole number.
// If the user input was not a valid whole number, ask them to enter a whole number.
WriteLine ( " Please enter a whole number. " );
// Convert the user input to an integer before returning it.
return ConvertToInteger (line);
height = ReadInteger ( " Enter your height in centimetres: " );
namespace UsefulUtilities
public static int ReadInteger ( string prompt)
// Prompt the user with the message
// Read the user input as a string.
string line = SplashKit . ReadLine ();
// Loop while the user's input is NOT a valid whole number.
while ( ! SplashKit . IsInteger (line))
// If the user input was not a valid whole number, ask them to enter a whole number.
SplashKit . WriteLine ( " Please enter a whole number. " );
line = SplashKit . ReadLine ();
// Convert the user input to an integer before returning it.
return SplashKit . ConvertToInteger (line);
public static void Main ()
height = ReadInteger ( " Enter your height in centimetres: " );
SplashKit . Write ( " You are " );
SplashKit . WriteLine ( " cm tall! " );
def read_integer ( prompt ) :
# Prompt the user with the message
# Read the user input as a string.
# Loop while the user's input is NOT a valid whole number.
while not is_integer ( line ):
# If the user input was not a valid whole number, ask them to enter a whole number.
write_line ( " Please enter a valid whole number. " )
# Convert the user input to an integer before returning it.
return convert_to_integer ( line )
height = read_integer ( " Enter your height in centimetres: " )
Manipulating strings
In addition to the functionality provided by each language’s standard library (The string
library in C++, and the System
library in C#), SplashKit’s utilities library provides some extra string manipulation functions that can assist you with manipulating string
data.
string location = " Burwood " ;
// Convert "Richard" to "RICHARD"
name = to_uppercase (name);
// Convert "RICHARD" to "richard"
name = to_lowercase (name);
// Remove all of the empty spaces at the start of " Burwood".
write_line ( " Before: " + location);
location = trim (location);
write_line ( " After: " + location);
using static SplashKitSDK . SplashKit;
string location = " Burwood " ;
//Convert "Richard" to "RICHARD"
name = ToUppercase (name);
//Convert "RICHARD" to "richard"
name = ToLowercase (name);
//Remove all of the empty spaces at the start of " Burwood".
WriteLine ( " Before: " + location);
location = Trim (location);
WriteLine ( " After: " + location);
namespace UsefulUtilities
public static void Main ()
string location = " Burwood " ;
//Convert "Richard" to "RICHARD"
name = SplashKit . ToUppercase (name);
SplashKit . WriteLine (name);
//Convert "RICHARD" to "richard"
name = SplashKit . ToLowercase (name);
SplashKit . WriteLine (name);
//Remove all of the empty spaces at the start of " Burwood".
SplashKit . WriteLine ( " Before: " + location);
location = SplashKit . Trim (location);
SplashKit . WriteLine ( " After: " + location);
# Convert "Richard" to "RICHARD"
name = to_uppercase ( name )
# Convert "RICHARD" to "richard"
name = to_lowercase ( name )
# Remove all of the empty spaces at the start of " Burwood".
write_line ( " Before: " + location )
location = trim ( location )
write_line ( " After: " + location )