current position:Home>Introduction to the most basic knowledge of C -- string

Introduction to the most basic knowledge of C -- string

2021-08-27 08:53:27 Learning design

This is my participation 8 The fourth of the yuegengwen challenge 20 God , Check out the activity details :8 Yuegengwen challenge

C# character string (String)

stay C# in , You can use character arrays to represent strings , however , A more common practice is to use string Keyword to declare a string variable .string Keywords are System.String Alias of class .

establish String object You can use one of the following methods to create string object :

  • By giving String Variable specifies a string
  • By using String Class constructor
  • By using the string concatenation operator ( + )
  • By retrieving properties or calling a method that returns a string
  • Convert a value or object to its string representation through formatting methods
 example 
using System;

namespace StringApplication
{
    class Program
    {
        static void Main(string[] args)
        {
           // character string , String connection 
            string fname, lname;
            fname = "Rowan";
            lname = "Atkinson";

            string fullname = fname + lname;
            Console.WriteLine("Full Name: {0}", fullname);

            // By using  string  Constructors 
            char[] letters = { 'H', 'e', 'l', 'l','o' };
            string greetings = new string(letters);
            Console.WriteLine("Greetings: {0}", greetings);

            // Method returns a string 
            string[] sarray = { "Hello", "From", "Tutorials", "Point" };
            string message = String.Join(" ", sarray);
            Console.WriteLine("Message: {0}", message);

            // Formatting method for converting values 
            DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
            string chat = String.Format("Message sent at {0:t} on {0:D}",
            waiting);
            Console.WriteLine("Message: {0}", chat);
            Console.ReadKey() ;
        }
    }
}
 Copy code 

When the above code is compiled and executed , It will produce the following results :

Full Name: RowanAtkinson Greetings: Hello Message: Hello From Tutorials Point Message: Message sent at 17:58 on Wednesday, 10 October 2012


String Attributes of a class

String Class has the following two properties :

Serial number The attribute name & describe
1 Chars At present String Get in object Char The specified location of the object .
2 Length In the current String Gets the number of characters in the object .

String Class method

String Class has many methods for string Operations on objects . The following table provides some of the most commonly used methods :

Serial number Method name & describe
1 public static int Compare( string strA, string strB )
Compare two specified string object , And returns an integer representing their relative position in the arrangement order . This method is case sensitive .
2 public static int Compare( string strA, string strB, bool ignoreCase )
Compare two specified string object , And returns an integer representing their relative position in the arrangement order . however , If the Boolean parameter is true , This method is case insensitive .
3 public static string Concat( string str0, string str1 )
Connect two string object .
4 public static string Concat( string str0, string str1, string str2 )
Connect three string object .
5 public static string Concat( string str0, string str1, string str2, string str3 )
Connect four string object .
6 public bool Contains( string value )
Returns a representation that specifies string The value of whether the object appears in the string .
7 public static string Copy( string str )
Creates a new string with the same value as the specified string String object .
8 public void CopyTo( int sourceIndex, char[] destination, int destinationIndex, int count )
from string Object starts copying the specified number of characters to Unicode The specified position in the character array .
9 public bool EndsWith( string value )
Judge string Whether the end of the object matches the specified string .
10 public bool Equals( string value )
Judge the current string Whether the object matches the specified string Object has the same value .
11 public static bool Equals( string a, string b )
Judge two specified string Whether the object has the same value .
12 public static string Format( string format, Object arg0 )
Replace one or more format items in the specified string with the string representation of the specified object .
13 public int IndexOf( char value )
Returns the specified Unicode The index of the first occurrence of a character in the current string , Index from 0 Start .
14 public int IndexOf( string value )
Returns the index of the first occurrence of the specified string in the instance , Index from 0 Start .
15 public int IndexOf( char value, int startIndex )
Returns the specified Unicode Character searches for the first occurrence of the index... Starting at the specified character position in the string , Index from 0 Start .
16 public int IndexOf( string value, int startIndex )
Returns the specified string and searches for the first occurrence of the index from the specified character position in the instance , Index from 0 Start .
17 public int IndexOfAny( char[] anyOf )
Returns a specified Unicode The index of the first occurrence of any character in the character array in this instance , Index from 0 Start .
18 public int IndexOfAny( char[] anyOf, int startIndex )
Returns a specified Unicode Any character in the character array searches for the first index from the specified character position in the instance , Index from 0 Start .
19 public string Insert( int startIndex, string value )
Returns a new string , among , The specified string is inserted in the current string The specified index location of the object .
20 public static bool IsNullOrEmpty( string value )
Indicates whether the specified string is null Or is it an empty string .
21 public static string Join( string separator, string[] value )
Connect all elements in a string array , Separate each element with the specified separator .
22 public static string Join( string separator, string[] value, int startIndex, int count ) Connect a specified element starting at a specified position in a string array , Separate each element with the specified separator .
23 public int LastIndexOf( char value ) Returns the specified Unicode The character is currently string The index position of the last occurrence in the object , Index from 0 Start .
24 public int LastIndexOf( string value )
Returns the current value of the specified string string The index position of the last occurrence in the object , Index from 0 Start .
25 public string Remove( int startIndex )
Remove all characters in the current instance , Start at the specified location , Until the last position , And return a string .
26 public string Remove( int startIndex, int count )
Removes a specified number of characters from the specified position of the current string , And return a string .
27 public string Replace( char oldChar, char newChar )
Put the present string In the object , All specified Unicode Replace the character with another specified Unicode character , And return the new string .
28 public string Replace( string oldValue, string newValue )
Put the present string In the object , All specified strings are replaced with another specified string , And return the new string .
29 public string[] Split( params char[] separator )
Returns an array of strings , Include the current string Object , The substring is specified using Unicode Separated by elements in a character array .
30 public string[] Split( char[] separator, int count )
Returns an array of strings , Include the current string Object , The substring is specified using Unicode Separated by elements in a character array .int Parameter specifies the maximum number of substrings to return .
31 public bool StartsWith( string value )
Determines whether the beginning of the string instance matches the specified string .
32 public char[] ToCharArray()
Returns a with the current string Of all characters in the object Unicode A character array .
33 public char[] ToCharArray( int startIndex, int length )
Returns a with the current string Of all characters in the object Unicode A character array , Start with the specified index , Until the specified length .
34 public string ToLower()
Converts a string to lowercase and returns .
35 public string ToUpper()
Converts a string to uppercase and returns .
36 public string Trim()
Remove current String All leading and trailing white space characters in the object .
The list of methods above is not exhaustive , Please visit MSDN library , See the complete list of methods and String Class constructor .

example The following example demonstrates some of the methods mentioned above :

Compare strings

 example 
using System;

namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string str1 = "This is test";
         string str2 = "This is text";

         if (String.Compare(str1, str2) == 0)
         {
            Console.WriteLine(str1 + " and " + str2 +  " are equal.");
         }
         else
         {
            Console.WriteLine(str1 + " and " + str2 + " are not equal.");
         }
         Console.ReadKey() ;
      }
   }
}
 Copy code 

When the above code is compiled and executed , It will produce the following results :

This is test and This is text are not equal.

The string contains the string

 example 
using System;

namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string str = "This is test";
         if (str.Contains("test"))
         {
            Console.WriteLine("The sequence 'test' was found.");
         }
         Console.ReadKey() ;
      }
   }
}
 Copy code 

When the above code is compiled and executed , It will produce the following results :

The sequence 'test' was found.

Get substring

 example 
using System;
namespace StringApplication
{
        class StringProg
        {
                static void Main(string[] args)
                {
                        string str = "Last night I dreamt of San Pedro";
                        Console.WriteLine(str);
                        string substr = str.Substring(23);
                        Console.WriteLine(substr);
                        Console.ReadKey() ;
                }
        }
}
 Copy code 

When the above code is compiled and executed , It will produce the following results :

Last night I dreamt of San Pedro San Pedro

Connection string

 example 
using System;

namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string[] starray = new string[]{"Down the way nights are dark",
         "And the sun shines daily on the mountain top",
         "I took a trip on a sailing ship",
         "And when I reached Jamaica",
         "I made a stop"};

         string str = String.Join("\n", starray);
         Console.WriteLine(str);
         Console.ReadKey() ;
      }
   }
}
 Copy code 

When the above code is compiled and executed , It will produce the following results :

Down the way nights are dark And the sun shines daily on the mountain top I took a trip on a sailing ship And when I reached Jamaica I made a stop


That's all for string learning today , See you next time !

copyright notice
author[Learning design],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210827085323471Z.html

Random recommended