To delete a string:
The string class provides a remove method for deleting a specified number of characters, starting at the specified position of a string, with the following syntax format:
Public String Remove (int startIndex)
Public String Remove (int startindex,int count)
which
StartIndex: Used to specify where to start the deletion, starting at 0 .
Count: Specifies the number of characters to delete.
The value of the parameter count cannot be 0 or negative (thestartIndex parameter cannot be a negative number), and if it is negative, it will throw a ArgumentOutOfRangeException Exception (the exception that is thrown when the parameter value exceeds the allowable range of values defined by the calling method); if 0, the deletion is meaningless, That is, the deletion was not done.
This method has two syntax formats, the first of which removes all characters from the specified position in the string to the last position. The second format removes the characters from the specified bibliography starting at the specified position in the string.
For example: Create a console program, declare a variable of type string str1, and initialize it: Download you for a lifetime. Then use the first syntax format of the Remove method to remove All characters that follow from index 3.
The code is as follows:
public static void Main (string[] args)
{
String str1= " download you for Life ";
String str2=str1. Remove (3);
Console.WriteLine (STR2);
Console.readkey ();
}
For example: Create a console application, declare a variable of type string str1, and initialize it: I love you flowers. The second syntax format of the Remove method is then used to remove two characters starting at index position 3.
public static void Main (string[] args)
{
String str1= " I Love you flowers ";
String str2=str1. Remove (3,2);
Console.WriteLine (STR2);
Console.readkey ();
}
To copy a string:
The string class provides the copy and CopyTo methods for copying a string or substring to another string or Char the array of type.
1. Copy method.
Creates a new instance of a string that has the same value as the specified string, with the syntax in the following format:
public static string Copy (String str)
STR: Is the string to copy.
return value: A string with the same value as Str.
For example: Create a console application, declare a variable of type string str1, and initialize it: I love you flowers. Then use the copy method to copy the string str1and assign the value to the string str2.
String str1= " I Love you flowers ";
String str2;
Str2=string.copy (STR1);
CopyTo Method
the CopyTo method has the same functionality as the copy method, but the CopyTo method can copy a portion of a string into another array. The syntax format is as follows:
public void CopyTo (int sourceindex,char[] Destination,int destinationindex,int count)
SourceIndex the starting position of the character to be copied.
Destination target character array
Destinationindex The starting position in the specified destination array
Count Specifies the number of characters to copy
Note: When the parameterSourceIndex,DdestinationindexorCountis a negative number, or the parameterCountgreater than fromStartIndexThe length of the substring to the end of this instance, or the parameterCountgreater than fromDestinationindexto theDestinationThe length of the end of the sub-array is raised when theArgumentOutOfRangeExceptionexception.
For example: Create a console application, declare a variable of type string str1, and initialize it: Download you for a lifetime. Then declare an array of type Char str2, using the CopyTo method to: Copy a lifetime download to the array str the. The code is as follows:
String str1= " download you for Life ";
Char[] Str2=new char[100];
Str1. CopyTo (1,str2,0,4);
Replacement string:
The string class provides a replace method for replacing a character or string in a string with another character or string. The syntax format is as follows:
public string Replace (char Ochar,char nchar)
public string Replace (string ovalue,string nvalue)
Ochar characters to be replaced
NCHAR the replaced character
Ovalue string to replace
Nvalue the replaced string
The first syntax format is primarily used to replace the characters specified in the string, and the second syntax format is primarily used to replace the string specified in the string.
For example: Create a console application that declares a string a one world,one dream replace The first syntax format for the method replaces "," in the string with " * replace The second syntax format of the method will be " one word "replaced with" one world
String A= "one World,one Dream";
String B=a.replace (', ', ' * ');
String C=a.replace ("One World", "One World");
This article is from the "Smile" blog, please be sure to keep this source http://yiyiweixiao.blog.51cto.com/2476874/1978632
46. My C # learning Note 12