String Special Characters


sTemp += "\r\n"		//Add a newline
String ^sTemp =  = "\x7";	// ‘\x’ means ‘0x’ and the 1 of 2 digits that follow are a hex value for the character requried

	newline( LF)		\n		horizontal tab (TAB)	\t
	vertical tab		\v		backspace		\b
	carriage return (CR)	\r		formfeed		\f
	alert (bell)		\a		backslash		\\
	question mark		\?		single quote		\'
	double quote		\"		null (NULL)		\0

You can do this:
MyString = "\x1b" "N" "\x1"; //N<1>

Length


	int Count = myString->Length;

Removing spaces (or other characters) from beginning and end of a string


	MyString = MyString->Trim();

Read Characters Within A String


	String ^MyString;

	Char a = Convert::ToChar(MyString[3]);

	if (Convert::ToChar(MyString[1]) != '.')

Find First Instance Of


	Index = TempString->IndexOf("\\");

Find Last Instance Of


	Index = TempString->LastIndexOf("\\");

Get string from character position # (Remove text from beginning or end of string)

Getting beginning of a string

	if (TempString->LastIndexOf("\\") >= 0)
		TempString = TempString->Substring(0, (TempString->LastIndexOf("\\")));
Getting end of string

	if (TempString->LastIndexOf("\\") >= 0)
		TempString = TempString->Substring(TempString->LastIndexOf("\\") + 1);

Get Characters Between Markers Within String


	String ^StartAfterChars;
	String ^EndBeforeChars;
	int Start;
	int Length;

	StartAfterChars = "?a=";
	EndBeforeChars = "&b=";
	Start = sTemp->IndexOf(StartAfterChars) + StartAfterChars->Length;
	Length = sTemp->IndexOf(EndBeforeChars) - Start;
	if ((Start > 0) && (Length > 0))
		UserId = sTemp->Substring(Start, Length);
	else
		UserId = "";

Insert Into String

	myString->Insert(0, "1234");	//Insert at character 0

Replace Characters In String


	myString->Replace(".", "!");	//Replace all . with !
	sTemp = sTemp->Replace('A', 'B');	//Replace a with B
	sTemp = sTemp->Replace("RemoveMe", "ReplaceWitMe");

Append To End Of String


	myString->Append("1234");
	myString->Append(gcnew array{'+', '+'});

Does string contain #


	if (MyString->Contains("ABCDEFG"))

Validating Text String Entry


	MyString = MyString->Trim();		//Remove leading or trailing spaces
	if (myString->Length > 30)
		myString = myString->Substring(0, 29);

	//Use these to force case:-
	//MyString = MyString->ToLower
	//MyString = MyString->ToUpper

Validating Numeric String Entry

Check for integer value entered

	String ^ValueEntered;
	ValueEntered = txtMyTextBox->Text;
	ValueEntered= ValueEntered->Trim();				//Remove leading and trailing spaces
	if (
	(ValueEntered->Length < 1) ||
	(!System::Text::RegularExpressions::Regex::IsMatch(ValueEntered, "^[0-9]*$"))
	)
		//Value is not numeric
Check for general numberic value entered

Use one of the solutions below as the best regex expression I have found is:

		"^[-0-9]*.[.0-9].[0-9]*$"

This is greate if you ensure the string as at least 3 digits long by adding leading zero's, but it will let through multiple '.' being entered.

An alternative solution uses a try, catch approach which isn't nearly as nice

Int32 myInt = 0;
try
{
	myInt = System::Convert::ToInt32 (myTextBox->Text);
}
catch (System::FormatException * pEx)
{
	myInt = 0; // or another default value
}
catch (System::OverflowException * pEx)
{
	myInt = 0; // or another default value
}
You could use the VB IsNumeric function by doing this, but not a 'nice' approach as you have to reference another dll

	#using
	...
	...
	if ( Microsoft::VisualBasic::Information::IsNumeric ( myTextBox->Text ) )
	   myInt = System::Convert::ToInt32 ( myTextBox->Text );
	else
	   MessageBox::Show ( "Value not numeric" );

There are lots of other tests in the String class

IndexOf Overloaded. Reports the index of the first occurrence of a String, or one or more characters, within this string.
IndexOfAny Overloaded. Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters.
LastIndexOf Overloaded. Reports the index position of the last occurrence of a specified Unicode character or String within this instance.
LastIndexOfAny Overloaded. Reports the index position of the last occurrence in this instance of one or more characters specified in a Unicode array.

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.