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
The sidebar is recommended
- Crazy blessing! Tencent boss's "million JVM learning notes", real topic of Huawei Java interview 2020-2021
- JS JavaScript how to get the subscript of a value in the array
- How to implement injection in vuex source code?
- JQuery operation select (value, setting, selected)
- One line of code teaches you how to advertise on Tanabata Valentine's Day - Animation 3D photo album (music + text) HTML + CSS + JavaScript
- An article disassembles the pyramid architecture behind the gamefi outbreak
- BEM - a front-end CSS naming methodology
- [vue3] encapsulate custom global plug-ins
- Error using swiper plug-in in Vue
- Another ruthless character fell by 40000, which was "more beautiful" than Passat and maiteng, and didn't lose BMW
guess what you like
-
Huang Lei basks in Zhang Yixing's album, and the relationship between teachers and apprentices is no less than that in the past. Netizens envy Huang Lei
-
He was cheated by Wang Xiaofei and Li Chengxuan successively. Is an Yixuan a blessed daughter and not a blessed home?
-
Zhou Shen sang the theme song of the film "summer friends and sunny days" in mainland China. Netizen: endless aftertaste
-
Pink is Wangyuan online! Back to the peak! The new hairstyle is creamy and sassy
-
Front end interview daily 3 + 1 - day 858
-
Spring Webflux tutorial: how to build reactive web applications
-
[golang] walk into go language lesson 24 TCP high-level operation
-
August 23, 2021 Daily: less than three years after its establishment, Google dissolved the health department
-
The female doctor of Southeast University is no less beautiful than the female star. She has been married four times, and her personal experience has been controversial
-
There are many potential safety hazards in Chinese restaurant. The top of the program recording shed collapses, and the artist will fall down if he is careless
Random recommended
- Anti Mafia storm: He Yun's helpless son, Sun Xing, is destined to be caught by his dry son
- Introduction to flex flexible layout in CSS -- learning notes
- CSS learning notes - Flex layout (Ruan Yifeng tutorial summary)
- Today, let's talk about the arrow function of ES6
- Some thoughts on small program development
- Talk about mobile terminal adaptation
- Unwilling to cooperate with Wang Yibo again, Zhao Liying's fans went on a collective strike and made a public apology in less than a day
- JS function scope, closure, let, const
- Zheng Shuang's 30th birthday is deserted. Chen Jia has been sending blessings for ten years. Is it really just forgetting to make friends?
- Unveil the mystery of ascension
- Asynchronous solution async await
- Analysis and expansion of Vue infinite scroll source code
- Compression webpack plugin first screen loading optimization
- Specific usage of vue3 video play plug-in
- "The story of huiyeji" -- people are always greedy, and fairies should be spotless!
- Installing Vue devtool for chrome and Firefox
- Basic usage of JS object
- 1. JavaScript variable promotion mechanism
- Two easy-to-use animation JS that make the page move
- Front end Engineering - scaffold
- Java SQL Server intelligent fixed asset management, back end + front end + mobile end
- Mediator pattern of JavaScript Design Pattern
- Array de duplication problem solution - Nan recognition problem
- New choice for app development: building mobile applications using Vue native
- New gs8 Chengdu auto show announces interior Toyota technology blessing
- Vieira officially terminated his contract and left the team. The national security club sent blessings to him
- Less than 200000 to buy a Ford RV? 2.0T gasoline / diesel power, horizontal bed / longitudinal bed layout can be selected
- How does "heart 4" come to an end? Pinhole was boycotted by the brand, Ma Dong deleted the bad comments, and no one blessed him
- We are fearless in epidemic prevention and control -- pay tribute to the front-line workers of epidemic prevention!
- Front end, netty framework tutorial
- Xiaomi 11 | miui12.5 | android11 solves the problem that the httpcanary certificate cannot be installed
- The wireless charging of SAIC Roewe rx5 plus is so easy to use!
- Upload and preview pictures with JavaScript, and summarize the most complete mybatis core configuration file
- [25] typescript
- CSS transform Complete Guide (Second Edition) flight.archives 007
- Ajax foundation - HTTP foundation of interview essential knowledge
- Cloud lesson | explain in detail how Huawei cloud exclusive load balancing charges
- Decorator pattern of JavaScript Design Pattern
- [JS] 10. Closure application (loop processing)
- Left hand IRR, right hand NPV, master the password of getting rich