Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the method of C # string String and character Char

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the C# string String and character Char method of how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you read this C# string String and character Char method how to use the article will have a harvest, let's take a look at it.

1. String: 1. Access the characters in String:

String itself can be thought of as an array of Char.

String s = "hello world"; for (int I = 0; I < s.Lengthi +) {Console.WriteLine (s [I]);} / or foreach (char c in s) {Console.WriteLine (c);}

Break into an array of characters (ToCharArray)

String s = "Hello World"; char [] arr = s.ToCharArray (); / / Console.WriteLine (arr [0]); / / outputs the first element of the array, outputting "H" 2, truncated substring: Substring (startIndex, [length]), including the characters at startIndex. String s = "hello world"; Console.WriteLine (s.Substring (3)); / / lo worldConsole.WriteLine (s.Substring (3,4)); / / lo w3. Find substrings: IndexOf (subString), LastIndexOf (subString), Contains (subString) string s = "hello world"; Console.WriteLine (s.IndexOf ("o")); / / 4Console.WriteLine (s.LastIndexOf ("o")); / / 7Console.WriteLine (s.IndexOf ('l')) / / find the position of the first occurrence of'l' in the string 2Console.WriteLine (s.IndexOf ('lags, 4)); / / find the position of the fourth occurrence of' l' in the string 9Console.WriteLine (s.IndexOf ('lure, 5,6)); / / locate the position of the occurrence of' l' between the fifth bit and the next six digits in the string 9Console.WriteLine (s.Contains ("e")); / / True4, fill the substring to the specified length: PadLeft (totalLength,char), PadRight (totalLength,char) string s = "hello world"; Console.WriteLine (s.PadLeft (15,'*)); / / * hello worldConsole.WriteLine (s.PadRight (15,'*')); / / hello world****5, convert case: ToUpper (), ToLower () string s = "Hello World" Console.WriteLine (s.ToUpper ()); / / HELLO WORLDConsole.WriteLine (s.ToLower ()); / / hello world6, delete the string fragment at the specified position of the string: Remove (startIndex,length) string s = "Hello World"; Console.WriteLine (s.Remove (7)); / / Hello WConsole.WriteLine (s.Remove (7Power1)); / / Hello Wrld7, replace substring: Replace (oldStr,newStr) string s = "Hello World"; Console.WriteLine ("l", "*")) / / He**o Wor*dConsole.WriteLine (s.Replace ("or", "*")); / / Hello W*ld8, remove leading and trailing spaces or specified characters: Trim (), TrimStart (), TrimEnd () string s = "Hello World"; Console.WriteLine (s.Trim ()); / / "Hello World" Console.WriteLine (s.TrimStart ()); / / "Hello World" Console.WriteLine (s.TrimEnd ()); / / "Hello World" string S1 = "hello Worldhd" Console.WriteLine (s1.Trim); / ello WorlConsole.WriteLine (s1.TrimStart); / ello WorldhdConsole.WriteLine (s1.TrimEnd); / / hello Worl9, insert characters before index: Insert (index,str) string s = "Hello World"; Console.WriteLine (s.Insert (6, "test")) / / Hello tests World10, splits the string into some special character (string), and returns an array of strings: Split (char/char [] / string [], [StringSplitOptions] string s = "Hello World"; Console.WriteLine (s.Split ('o')); / / "Hell", "W", "rld" Console.WriteLine (s.Split ('eBay,' d')) / / "H", "llo Wor l", "Console.WriteLine (s.Split (new string [] {"}, StringSplitOptions.None)); / /" Hello "," Wor ","," ld "," Console.WriteLine (s.Split (new string [] {"}, StringSplitOptions.RemoveEmptyEntries)); / /" Hello "," Wor "," ld "foreach (string sub in s.Split ('o')) {Console.WriteLine (sub) } 11. String merging

1. String.Concat (str1, str2, … ., strn): concatenates n strings with no connector in the middle. String concatenation can also be implemented with'+'.

2. String list (string [], IEnumerable) is concatenated with str, and the static function Join (SplitCh, array)

String [] arr = {"Hello", "World"}; Console.WriteLine (string.Join ("*", arr)); / / Hello*World

3. Instance method StringBuilder.Append

StringBuilder sb = new StringBuilder (); / / declare a string constructor instance sb.Append ("A"); / / use string constructor to connect strings for higher performance sb.Append ('B'); Console.WriteLine (sb.ToString ()); / / output "AB" 12, formatting, static function Format (placeholder, variable) string s = "Hello World" Console.WriteLine (string.Format ("I Love\" {0}\ ", s)); / / I Love" Hello World "Console.WriteLine (string.Format (" {0Love C} ", 120.2)); / / ¥120.20Console.WriteLine (string.Format (" {0: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

1. Int32.TryParse (string,out bool): always does not throw an exception and returns true/false to indicate whether the parsing is successful. String returned unsuccessful parsing for null.

Int ret = 0 if (int.TryParse ("120.5", out ret)) / / conversion exception! {Console.WriteLine (ret);} else {Console.WriteLine ("conversion exception!") ;}

2. Int32.Parse (string): an exception will be thrown if the parsing is not successful. Throws a "value cannot be null. Parameter name: String" exception when string is null.

Try {Console.WriteLine (int.Parse ("120.5")); / / incorrect format of input string} catch (Exception e) {Console.WriteLine (e.Message);}

3. Convert.ToInt32 (string): an exception will be thrown if the parsing is not successful, but 0. 0 will be returned if the string is null.

Try {Console.WriteLine (Convert.ToInt32 ("120.5")) / / the input string is not in the correct format. } catch (Exception e) {Console.WriteLine (e.Message);} 14. Comparison of strings:

1. Compare () is the static version of CompareTo (), which returns a string that is less than, greater than, or equal to the latter (- 1, respectively).

String str1 = "you are very happen!"; string str2 = "I am very happen!"; Console.WriteLine (string.Compare (str1, str2)); / / 1Console.WriteLine (str1.CompareTo (str2)); / / 1

2. CompareOrdinal (): compare two strings, regardless of localized language and culture. Divide the entire string into groups of 5 characters (10 bytes), compare them one by one, find the first different ASCII code and exit the loop. And find out the difference between the two ASCII codes.

String str1 = "you are very happen!"; string str2 = "I am very happen!"; Console.WriteLine (string.CompareOrdinal (str1, str2)); / / 48

3. Equals () is equivalent to "= =", static or instance Equals, return equal or unequal (true/false).

String str2 = "I am very happen!"; string str3 = "I am very happen!"; Console.WriteLine (str2.Equals (str3)); / / TrueConsole.WriteLine (str2 = = str3); / / True15, copy of string

(1), String.Copy (str): the parameter str is the string to be copied, and it returns a string equal to the string

(2), SreStr.CopyTo (StartOfSreStr, DestStr, StartOfDestStr, CopyLen): it must be called by the string instance to be copied, and it can copy a part of it to the specified location of the target string.

String s = "Hello World"; string S1 = String.Copy (s); Console.WriteLine (S1); / / Hello Worldchar [] S2 = new char [20]; s.CopyTo (2, S2, 0, 8); Console.WriteLine (S2); / / llo Worl00000000000000000000000016, determine whether a string has started or ended string s = "Hello World"; Console.WriteLine (s.StartsWith ("He")); / / TrueConsole.WriteLine (s.EndsWith ("He")) / / False17, determine whether the character is null or empty, and the return value is bool String s = "Hello World"; string S2 = null;Console.WriteLine (string.IsNullOrEmpty (s)); / / False Console.WriteLine (string.IsNullOrEmpty (S2)); / / True II, char character. 1. The representation of Char:

Literally: char axioms x'

Hexadecimal method: char asides'\ x0058'

Show converted integers: char a = (char) 88

Unicode form: char aversion'\ u0058'

2. Escape characters:

\ n =\ u000a newline character

\ t =\ u0009 tab character

\ r =\ u000d carriage return

\ "=\ u0022 double quotes

\ u0027 single quotation marks

\ u005c backslash

\ b =\ u0008 backspace character

3. Static method of char:

Char.IsDigit (): whether it is a decimal number

Char.IsNumber (): number

Char.IsLetter (): letter

Char.IsLetterOrdigt (): letters or arrays

Char.IsUpper () / IsLower (): upper and lowercase letters

Char.IsPunctuation (): punctuation

Char.IsSymbol (): symbol character

Char.IsControl (): control character

Char.IsSeprator (): delimiter

Char.IsWhiteSpace (): White space character

Char.GetNumberialValue (): get a numeric value

Char.ToUpper () / ToLower (): change case

This is the end of the article on "how to use the C # string String and character Char". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use C # string String and character Char". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report