C# combat learning (6) - String

   Today we focus on learning strings in C#. Before learning, I would like to ask what is a string? A string or string (String) is a string of characters consisting of numbers, letters, and underscores . It is a data type in programming languages ​​that represents text. In programming, a string is a continuous sequence of symbols or values, such as a string of symbols (a string of characters) or a string of binary digits (a string of binary numbers).

1. In C#, strings are read-only and can be used as read-only arrays of char type. Since they are read-only, the values ​​in them cannot be changed (immutability), but we have requirements, so by Make changes as follows. - Create a new string (it did not change at this time, but pointed to a new address)

 string - array - string (read-only string will change value and become string again)

static void Main(string[] args)
        { //Change the read-only string to an array, and change the value to a new string again
            string text = "I'm so handsome";
            char[] chs = text.ToArray();//Programming string array
            chs[1] = 'very';//change the value
            string str = new string(chs);//Into a new string
            Console.WriteLine(str);
            Console.ReadKey();
        }

How to convert between upper and lower case of letters in a string?

Reminder: The string is immutable. When modifying, the function will not directly modify the content of the string, but return the value of the modified string through the return value of the function.

  ToLower(): Convert the string to lowercase,  ToUpper(): Convert the string to uppercase    Trim(): Remove the blanks on both sides of the string

static void Main(string[] args)

        {
            Console.WriteLine("Please enter your first English name");
            string str1 = Console.ReadLine();
            str1 = str1.ToLower();//Change the input letters to lowercase
            str1 = str1.ToUpper();//Change the input letters to uppercase       
            Console.WriteLine("Please enter your second English name");
            string str2= Console.ReadLine();
            str2 = str2.ToLower();//Change the input letters to lowercase
            str2 = str2.ToUpper();//Change the input letters to uppercase
            if (str1==str2)
            {
                Console.WriteLine("Same name: {0}", str1);
            }
            else
            {
                Console.WriteLine("The name is different: {0}-----{1}", str1,str2);
            }
            Console.ReadKey();
        }

 Ignore the comparison of size values: bool result = str1.Equals(str2, StringComparison.OrdinalIgnoreCase);

static void Main(string[] args)
        {
            Console.WriteLine("Please enter your first English letter");
            string str1 = Console.ReadLine();     
            Console.WriteLine("Please enter your second English letter");
            string str2= Console.ReadLine();
            // ignore case comparison, return value is boolean
            bool result = str1.Equals(str2, StringComparison.OrdinalIgnoreCase);
            if (true)
            {
                Console.WriteLine("Same letter: {0}",str1);
            }
            else
            {
                Console.WriteLine("Different letters: {0}-----{1}", str1,str2);
            }
            Console.ReadKey();
        }

Remove unwanted content from the string:

string str = "you-- well, good--long time--see--";
 char[] chs = new char[] {' ','-' };//eliminate unwanted items in the string characters, (this process just eliminates unwanted characters, and blank items will be generated after elimination)
string[] result = str.Split(chs, StringSplitOptions.RemoveEmptyEntries);//Remove blank items
for (int i = 0; i < result.Length; i++)
{
   Console.Write(result[i]);
}              
   Console.ReadKey();

Any type can be converted to string type data through the ToSring() method

int num = 100;
 string Str = num.ToString();//Convert int type data to string type data
Console.WriteLine(Str);
Console.ReadKey();
replace substring in string
string name = "Jiaxing is so handsome";
 name = name.Replace("good", "very");//Replace "good" in the string with "very", the characters in the string before the comma , followed by the replaced character
Console.WriteLine(name);
Console.ReadKey();
Determine when a string contains a certain character
string name = "Jiaxing is ugly";
 bool result = name.Contains("ugly");//Determine whether the string contains the word "handsome"
if(result)
{
     name = name.Replace("ugly", "handsome");//Character replacement
     Console.WriteLine(name);
}
else
{
     Console.WriteLine("There is no such obvious word");
}         
     Console.ReadKey();

Intercept a part of a string

string str = "Peppa pig tattoo, applause to Jiaxing"; str = str.Substring(8);//The string after the eighth character is intercepted, the premise: the string cannot exceed the string before interception The length is the index value

Console.WriteLine(str);             
Console.ReadKey();
string str = "Child, be obedient, you should spank if you are not obedient. Hahahahahahahaha, you are not obedient";
 str=str.Substring(str.Length - 4);//If the string is too long, use characters The length of the string is truncated
Console.WriteLine(str);
Console.ReadKey();

Check if a string starts or ends with a certain character

string str = "Hello";
 bool result = str.StartsWith("you");//Determine whether the string starts with the character you
// bool result = str.EndsWith("ah");//Determine whether the string starts with the character ah
if (result)
{
   Console.WriteLine("The string starts with this character, the string is: {0}",str);              
}
else
{
   Console.WriteLine("String does not exist");
}         
Console.ReadKey();

How to remove spaces:

trim(): remove the spaces before and after the string trimEnd(): remove the spaces after the string trimStart(): remove the spaces before the string


There are many more methods for strings, so let's talk about them today! The little brothers and sisters who come in remember to give a like!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324780521&siteId=291194637