# 1.8.2 String Data

## String Variables

string str1; // Declare a string variable named str1
str1 = "New York"; // Assigns to str1 a character sequence

string str2 = "Tokyo"; // Declare a string variable and assignment

// Declare a character array and initialize with a character sequence
char ch[] = "This is a test!";

// Declare a character array, set size and initialize with a character sequence
char chArr[255] = "Big World.";

## Convert String to Numeric

string str = PI; // Assigns a numeric value to string variable

// Convert string to numeric
double dd = atof(str, true);
out_double("dd=", dd); // dd=3.14159

// Convert string to complex
str = "1+2.5i";
complex cc = atoc(str);
out_complex("cc = ", cc); // cc = 1.000000+2.500000i

// Convert string to int
str = "100";
int nn = atoi(str);
out_int("nn = ", nn); // nn = 100

## Append Numeric/String to another String

// Append numeric or string to another string
// In Origin C, support use '+' to add a numeric/string type const or variable
string str = "The area is " + 30.7; // Append a double type const to string

str += "\n"; // Append a string const to string variable

int nLength = 10;
str += "The length is " + nLength; // Append a int type variable to string

out_str(str);

## Find Sub String

// Find and get sub string
string str = "[Book1]Sheet1!A:C";
int begin = str.Find(']'); // Find and return the index of ']'
begin++; // Move to the next character of ]

int end = str.Find('!', begin); // Find and return the index of '!'
end--; // Move the previous character of !

// Get the sub string with the begin index and substring length
int nLength = end - begin + 1;
string strSheetName = str.Mid(begin, nLength);
out_str(strSheetName);// Should output "Sheet1"

## Replace Sub String

// Find and replace one character
string str("A+B+C+");
int nCount = str.Replace('+','-');
out_int("", nCount); // nCount will be 3
out_str(str); // "A-B-C-"

// Find and replace a character string
str = "I am a student.\nI am a girl.";
nCount = str.Replace("I am", "You are");
out_int("", nCount); // nCount will be 2
out_str(str);

## Path String Functions

### File Path String

// string::IsFile is used to check the file if exist
string strFile = "D:\\TestFolder\\abc.txt";
bool bb = strFile.IsFile();
printf("The file %s is %sexist.\n", strFile, bb ? "" : "NOT ");

// GetFilePath function is used to extract the path from a full path string
string strPath = GetFilePath(strFile);
out_str(strPath);

// GetFileName function is used to extracts the file name part
// from a string of full path
bool bRemoveExtension = true;
string strFileName = GetFileName(strFile, bRemoveExtension);
out_str(strFileName);

// string::IsPath to check if the path is exist
bb = strPath.IsPath();
out_int("", bb);

### Origin System Path

string strSysPath = GetOriginPath(ORIGIN_PATH_SYSTEM);
printf("Origin System Path: %s\n", strSysPath);

string strUserPath = GetOriginPath(ORIGIN_PATH_USER);
printf("User File Path: %s\n", strUserPath);