Copy string (string str)
The Copy method can Copy a string to another string.
Syntax: publicstaticstring Copy (stringstr)
Parameter: str: the character to be copied.
[Csharp]
String Str1 = "My name is James ";
Console. WriteLine (string. Copy (Str1); // output: Zhang San
Console. ReadKey ();
Join String. Join
[Csharp]
String Str1 = "My name is James ";
String Str2 = "A Chinese ";
String S = ",";
String [] strArrary = {Str1, Str2 };
StringnewString = String. Join (S, strArrary );
Console. WriteLine (newString); // output: I'm Zhang San, a Chinese
Console. ReadKey ();
Remove the Trim, TrimStart, and TrimEnd strings.
To remove a set of specified characters at the start or end of a string, you can use the Trim, TrimStart, and TrimEnd methods.
Syntax:
Public string Trim (paramschar [] trimChars)
Public string TrimStart (paramschar [] trimChars)
Public string TrimEnd (paramschar [] trimChars)
Parameter: trimChars: array of Unicode characters to be removed or empty reference
// Remove space
Stringstr = "I"; // defines a string with spaces at the front and end.
Console. WriteLine ("[" + str. TrimEnd () + "]"); // remove the following space and output: "[I ]"
Console. WriteLine ("[" + str. TrimStart () + "]"); // go to the front space and output: "[I ]"
Console. WriteLine ("[" + str. Trim () + "]"); // remove leading and trailing spaces and output: "[Me ]"
// Remove a single special character
Stringstr1 = "* Who are you *";
Console. WriteLine (str1.TrimEnd ('*'); // go to the end * and output: "* Who are you"
Console. WriteLine (str1.TrimStart ('*'); // go to the front * and output: "Who are you *"
Console. WriteLine (str1.Trim ('*'); // before and after *, output: "Who are you"
// Remove multiple special characters
Stringstr2 = "* % * China's rise * % ";
Console. WriteLine (str2.TrimEnd (newChar [] {'*', '%'}); // remove the following symbol and output: "* % * China has risen"
Console. WriteLine (str2.TrimStart (newChar [] {'*', '%'}); // remove the preceding symbol and output: "China has risen ** %"
Console. WriteLine (str2.Trim (newChar [] {'*', '%'}); // remove the front and back symbols and output: "China has risen"
Console. ReadKey ();
Delete string Remove (int startIndex, int count)
The Remove method is used to delete specified characters at a specified position of a string.
Syntax: publicstring Remove (int startIndex, intcount)
Parameters:
StartIndex: Start to delete the character position, starting from 0
Count: the number of characters to delete.
String str = "abcde ";
Console. WriteLine (str. Remove (2, 2); // output: abe
Console. ReadKey ();
Fill string PadLeft (int totalWidth, charpaddingChar)
You can use the PadLeft/PadRight method to add a specified number of spaces or custom characters to the string for right or left alignment.
Syntax:
Public string PadLeft (int totalWidth, charpaddingChar)
Public string PadRight (int totalWidth, charpaddingChar)
Parameters:
TotalWidth: the number of characters in the result string, equal to the number of original characters plus the number of characters filled.
PaddingChar: filled character.
In this example, the String-type PadLeft/PadRight method is used to fill in characters on the left and right sides of the String "Tomorrow technology.
The Code is as follows:
String str = "abcde ";
Console. WriteLine (str. PadLeft (8, '*'); // output: *** abcde
Console. WriteLine (str. PadRight (10, '%'); // output: abcde %
Console. ReadKey ();
Insert string Insert (int startIndex, stringvalue)
The Insert method is used to Insert another string at a specified position in a string to construct a new string.
Syntax:
Public stringInsert (int startIndex, stringvalue)
Parameters:
StartIndex: The index location to insert.
Value: the character to be inserted.
Truncation string Substring (int startIndex, intlength)
You can use the Substring method to extract substrings from a specified string.
Syntax:
Public stringSubstring (int startIndex, int length)
Parameters:
StartIndex: Index of the starting position of the substring.
Length: the number of characters in the substring.
String location IndexOf (string value)
You can use the IndexOf method to locate the first occurrence of a character or substring in a string.
Syntax:
Public intIndexOf (string value)
The parameter value is the character or substring to be located. If this character is found, it is the index location of the parameter value. If this character is not found, it is-1. If the parameter is null, the return value is 0.
Replace string (string oldValue, stringnewValue)
The Replace method can Replace certain characters or substrings in a string.
Syntax:
Public stringReplace (string oldValue, string newValue)
Parameters:
OldValue: the character to be replaced.
NewValue: the character that replaces all matches of oldValue.
Comparison string
The String class provides a series of methods for String comparison. The following describes these methods.
Int CompareTo (string strB)
Used to compare whether two strings are equal.
Syntax:
Public intCompareTo (string strB)
For example, compare the string stra with the string strb. The Code is as follows:
Stra. CompareTo (strb)
If the value of stra is equal to the value of strb, 0 is returned. If the value of stra is greater than the value of strb, 1 is returned. Otherwise,-1 is returned.
Bool Equals (string value)
Used to determine whether two String objects have the same value.
Syntax:
Public boolEquals (string value)
For example, determine whether the strings stra and strb are equal. The Code is as follows:
Stra. Equals (strb)
True if the value of stra is the same as that of strb; otherwise False.
Note: The Equals method is case sensitive.
Split string
The Split method Splits a string into several small strings using a separator.
Syntax:
Public string [] Split (paramschar [] separator)
Parameters:
Params: an array of separators that separate strings.
// Do not separate spaces
StringsplitStr = "a, B, c, d ";
String [] arrSplitStr1 = splitStr. Split (',');
For (int I = 0; I <arrSplitStr1.Length; I ++)
{
Console. WriteLine (arrSplitStr1 [I]); // output five numbers respectively: a, B, c, and d
}
// Separate by Spaces
String [] arrSplitStr2 = splitStr. Split (newChar [] {','}, StringSplitOptions. RemoveEmptyEntries );
For (int I = 0; I <arrSplitStr2.Length; I ++)
{
Console. WriteLine (arrSplitStr2 [I]); // output four numbers: a, B, c, and d.
}
String. Format
Format is a static method of String, with a total of five reloads.
Public staticstring Format (stringformat, Object obj)
Public staticstring Format (stringformat, Object objA, Object objB)
Public staticstring Format (stringformat, Object objA, Object objB, Object objC)
Public staticstring Format (stringformat, params Object [] args)
Public staticstring Format (IFormatProvider provider, string format, paramsObject [] args)
The format parameter indicates the format, obj indicates the string to be formatted, and provider indicates that it can be used to provide cultural-specific formatting information.
The following example illustrates the usage
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace StringDemo
{
ClassProgram
{
Staticvoid Main (string [] args)
{
// Stringstr = "1 ";
// Numeric output
Console. WriteLine (string. Format ("{0: C2}", 2); // C2 indicates the currency, where 2 indicates the digits after the decimal point
Console. WriteLine (string. Format ("{0: D2}", 2); // D2 indicates the decimal digits, of which 2 indicates the digits, with less than 0 placeholder
Console. WriteLine (string. Format ("{0: E3}", 22233333220000); // E3 indicates the scientific notation, where 3 indicates the number of digits retained after the decimal point
Console. WriteLine (string. Format ("{0: N}", 2340000); // N indicates a number separated by a semicolon
Console. WriteLine (string. Format ("{0: X}", 12345); // X indicates hexadecimal
Console. WriteLine (string. Format ("{0: G}", 12); // regular output
Console. writeLine (string. format ("{0: 000. 00} ", 12); // output in formatted format (000.00-> 012.00, 0.0-> 12.0)
Console. WriteLine (string. Format ("{0: F3}", 12); // F floating point type, where 3 indicates the number of decimal places
Console. ReadLine ();
}
}
}
The output result is
¥2.00
02
2.223E + 013
2,340,000.00
3039
12
012.00
12.000