program String_manipulation;
@use "system/str"
var
	longString : string ="I MEOW YOU KNOW";
	splitString : array[64] of byte;
	// Could also have been
	// splitString : string = "                                                                ";
	sp: pointer; // Pointer to a string
	// Temporary buffer
	buf : array[256] of byte;

	ii : integer = 12345;
	
	lst : array[] of string = ("NEED","MORE","CATNIP");

/*
	In this example, we use various string methods to manipulate
	the string "longString". Examples include:
		- String splitting
		- Comparing strings
		- getting strings from index in string lists

*/


begin
	clearscreen(key_space,screen_char_loc);
	
	moveto(5,2,hi(screen_char_loc));
	printstring(#longString,0,40);

	// Splits "longstring" with token " ", save in "splitString"
	strsplit(#longString,#splitString,key_space);
	// sp now points to the first word in the string list
	sp:=strgetfromindex(#splitString,0);
	// strcmp compares a string/pointer to another string/pointer
	if (strcmp(sp,"I")) then 
	begin
		moveto(5,5,hi(screen_char_loc));
		printstring("ALWAYS ABOUT YOURSELF...",0,40);
	end;	
	
	moveto(7,8,hi(screen_char_loc));
	sp:=strgetfromindex(#splitString,1);
	printstring(	sp,0,40);

	moveto(9,10,hi(screen_char_loc));
	sp:=strgetfromindex(#splitString,3);
	printstring(	sp,0,40);

	moveto(11,12,hi(screen_char_loc));
	sp:=strgetfromindex(#splitString,2);
	printstring(	sp,0,40);


	// Using the new global string library (not finished yet)
	
	moveto(11,14,hi(screen_char_loc));
	// convert the number "ii" to decimal:
	str::itoa(ii, #buf, 10);
	printstring(	buf,0,40);
	
	
	
	moveto(19,14,hi(screen_char_loc));
	// convert the number in "ii" to hex
	str::itoa(ii, #buf, 16);
	printstring(	buf,0,40);
	

	moveto(10,17,hi(screen_char_loc));
	// Copy a substring to the buffer ("MEOW")
	str::substr(#buf, #longString, 2,4);
	printstring(	buf,0,40);
	
	moveto(0,19,hi(screen_char_loc));
	// Copy a substring to the buffer ("MEOW")
	printstring(	lst[0],0,40);
	screenmemory+=10;
	printstring(	lst[1],0,40);
	screenmemory+=10;
	printstring(	lst[2],0,40);
	
	
	Loop();

end.
