3.1.3 String



Version Info

Minimum Origin Version Required: Origin8 SR0

Remark

This section introduces character and string manipulation.

For more details please refer to the following Origin C Reference sections:

Character and String Manipulation Global Functions

Origin C String Class

Combine

Combine String and Integer

Run "str_combine_ex1(abc, 1)" in Command Window to print out abc_1.

void str_combine_ex1(string str, int nn)
{
	out_str(str + "_" + nn);
}

Combine String and Double

Run "str_combine_ex2(abc, PI)" in Command Window to print out abc_3.14.

void str_combine_ex2(string str, double dd)
{
	string str2;
	str2.Format("%s_%.2f", str, dd);
	out_str(str2);
}

Combine Two Strings

void str_combine_ex3()
{
    char    *ptr = NULL;
    char    str1[80] = "This is the first part. ";
    string  str2 = "This is the second part.";

    // appends str2 to str1, so the buffer of str1 must be large enough to contain both strings. 
    ptr = lstrcat(str1, str2); // return the pointer of str1
    printf(ptr);
}
void str_combine_ex4()
{
    string  str1 = "This is the first part. ";
    string  str2 = "This is the second part.";	
    string str = str1 + str2;
    out_str(str);
}

Compare

Compare Case Sensitive

Compare two strings with case sensitivity. Run "str_compare_ex1(ABC, abc)" in Command window to print out Not equal.

void str_compare_ex1(string str1, string str2)
{
	int nn = strcmp(str1, str2); // strcmp is case sensitive
	if( 0 == nn )
		out_str("Equal");	
	else
		out_str("Not equal");
}

Compare Not Case Sensitive

Compare two strings without case sensitivity. Run "str_compare_ex2(ABC, abc)" in Command window to print out Equal.

void str_compare_ex2(string str1, string str2)
{
	int nn = lstrcmpi(str1, str2); // lstrcmpi is not case sensitive
	if( 0 == nn )
		out_str("Equal");	
	else
		out_str("Not equal");
}

Find

Find a string within a string. Run str_find_ex1("To be or not to be", "be") to find the locations of "be".

void str_find_ex1(string str1, string str2)
{
	printf("%s\n", str1);
	int iPos = str1.Find(str2);
	while(iPos > -1)
	{
		printf("%s found at %u\n", str2, iPos + 1); // 0-based index
		iPos = str1.Find(str2, iPos + 1); // Next
	}
}

Delete

Delete a character or characters from a string.

Run str_delete_ex1("To be or not to be", "be") to delete instances of "be.

void str_delete_ex1(string str1, string str2)
{
	printf("%s\n", str1);
	int iPos = str1.Find(str2);
	while(iPos > -1)
	{
		str1.Delete(iPos, str2.GetLength());
		iPos = str1.Find(str2);
	}
	printf("%s\n", str1);
}

Replace

Replace a character or characters in a string.

Run str_replace_ex1("Pluto is a planet.", "planet", "dwarf planet").

// Replace characters
void str_replace_ex1(string str1, string str2, string str3)
{
	printf("%s\n", str1);
	str1.Replace(str2, str3);
	printf("%s\n", str1);
}