Working with strings. Class System.String
- 1. How initialize the variable of type String?
- 2. How to clear string of type String?
- 3. How to determine whether two strings are equal?
- 4. How to compare two string of type String in lexicographical order?
- 5. How combine two strings of type String?
- 6. How copy one string into another string?
- 7. Inserting a substring starting at the specified index. Method Insert().
- 8. Finding and returning the index of the first occurrence of a substring in the given string.
- 9. Search the index of last occurrence of substring in the string by method LastIndexOf().
- 10. How to define the length of string?
- 11. Creating a string of predetermined width. Demonstration of functions PadLeft() and PadRight().
- 12. Deleting the specified number of characters from string by method Remove().
- 13. Replacing the characters in string. Method Replace().
- 14. How to select the substring in string. Method Substring().
- 15. Converting the string which represented as number into the integer. Method Parse().
- 16. Converting the integer into string. Method ToString().
- 17. Converting string into the floating point number.
- 18. Converting the floating point number into string.
- 19. Converting the string into the value of bool type.
- Related topics
Search other websites:
Answers
Class System.String represents text as a series of Unicode characters.
1. How initialize the variable of type “String“?
String ^s1 = "873"; // s1 = "873"
⇑
2. How to clear string of type “String“?
String ^s1 = "Hello!"; String ^s2; s2 = s1; // s2 = "Hello!" s2 = s1->Empty; // s2 = ""
⇑
3. How to determine whether two strings are equal?
String ^s1 = "Hello!"; String ^s2 = "Hello!"; int res; if (s1 == s2) res = 1; else res = 0;
⇑
4. How to compare two string of type “string” in lexicographical order?
In this case it is needed to use method Compare(). Method returns -1, 0 or 1. In the given example is shown the result of work the method.
String ^s1 = "Hello!"; String ^s2 = "Hello"; int res; res = s1->Compare(s1,s2); // res = 1 res = s2->Compare(s2,s1); // res = -1 res = s2->Compare(s1,s2); // res = 1 s2 = s1; res = s1->Compare(s1,s2); // res = 0 res = s1->Compare(s2,s1); // res = 0
⇑
5. How combine two strings of type “string”?
Way #1. Method Concat().
String ^s1 = "Hello "; String ^s2 = "world!"; String ^s3; s3 = s1->Concat(s1,s2); // s3 = "Hello world!"
Way #2. Operator “+“.
String ^s1 = "Hello "; String ^s2 = "world!"; String ^s3; s3 = s1 + s2; // s3 = "Hello world!"
⇑
6. How copy one string into another string?
Way #1. Assignment operator.
String ^s1 = "My String"; String ^s2; s2 = s1; // s2 = "My String"
Way #2. Method Copy().
String ^s1 = "MyString "; String ^s2; s2 = s1->Copy(s1); // s2 = "My String"
⇑
7. Inserting a substring starting at the specified index. Method Insert().
To insert a substring need to use method Insert(). Method gets two parameters. First parameter is position of index in string, from which inserting is done (strarts with 0). Second parameter – text of the same string.
String ^s1 = "123789"; String ^s2; s2 = s1->Insert(3, "456"); // s2 = "123456789" s1 = "123789"; s2 = s1->Insert(0, "456"); // s2 = "456123789" // error with message: // "Specified argument was out of the range of valid values" // "Parameter name: startIndex" // s2 = s1->Insert(19, "456");
If set the index to out of range, then will be generated exception.
⇑
8. Finding and returning the index of the first occurrence of a substring in the given string.
This is realized by method IndexOf(). If substring is found in the string then method returns a position of first occurrence. Otherwise method returns -1. Method has the overloaded realizations.
String ^s1 = "Hello world!"; int index; index = s1->IndexOf("wor"); // index = 6 index = s1->IndexOf("ab"); // index = -1 index = s1->IndexOf("wor",7); // index = -1 - search from position 7 index = s1->IndexOf("wor",0); // index = 6 - search from position 0 // search starts from specified index (3) // and being verified the specified number of characters (5) index = s1->IndexOf("wo",3,5); // index = 6
⇑
9. Search the index of last occurrence of substring in the string by method LastIndexOf().
String ^s1 = "text-text-text"; int index; index = s1->LastIndexOf("text"); // index = 10
⇑
10. How to define the length of string?
To define the length of string (number of characters) is used property Length.
String ^s1 = "abcdef"; int len; len = s1->Length; // len = 6 s1 = ""; len = s1->Length; // len = 0
⇑
11. Creating a string of predetermined width. Demonstration of functions PadLeft() and PadRight().
Functions PadLeft() and PadRight() are used for creating the formatted string, in which the right positions and left positions are filled by spaces.
String ^s1 = "abc"; String ^s2; s2 = s1->PadLeft(5); // s2 = " abc" s2 = s1->PadLeft(2); // s2 = "abc" s2 = s1->PadLeft(9); // s2 = " abc" // error "Non-negative number required." // s2 = s1->PadLeft(-2); s2 = s1->PadRight(3); // s2 = "abc" s2 = s1->PadRight(8); // s2 = "abc "
⇑
12. Deleting the specified number of characters from string by method Remove().
String ^s1 = "0123456789"; String ^s2; s2 = s1->Remove(3,2); // s2 = "01256789"
⇑
13. Replacing the characters in string. Method Replace().
String ^s1 = "0123456789"; String ^s2; s2 = s1->Replace(s1,"22"); // s2 = "22"
⇑
14. How to select the substring in string. Method Substring().
Method Substring() has two realizations.
String ^s1 = "Automobile"; String ^s2; // way #1 - Selection the substring from given position to the end of string. s2 = s1->Substring(4); // s2 = "mobile" // way #2 - Selection the substring from given position (parameter #1) // to the specified number of characters (parameter #2) s2 = s1->Substring(0,4); // s2 = "Auto"
⇑
15. Converting the string into the integer which represented as number. Method Parse().
// String => int int i; String ^s1 = "280"; i = i.Parse(s1); // i = 280 i = i.Parse("-32"); // i = -32
⇑
16. Converting the integer into string. Method ToString().
int i; String ^s1; // int => String i = 350; s1 = i.ToString(); // s1 = 350
⇑
17. Converting string into the floating point number.
When you convert string into corresponding real number, it is important to take into account the character encoding.
// String => double double x; String ^s1; s1 = "12,87"; x = x.Parse(s1); // x = 12.87
⇑
18. Converting the floating point number into string.
// double => String double x; String ^s1; x = -3.568; s1 = x.ToString(); // s1 = "-3.568
⇑
19. Converting the string into the value of “bool” type.
// String => bool bool b; String ^s1; s1 = "true"; b = b.Parse(s1); // b = True