search
HomeBackend DevelopmentC#.Net TutorialSummary of methods for operating strings in C#

This article mainly introduces the C# operation string method summary example code. Friends who need it can refer to it

No more nonsense, the specific code is as follows:


staticvoid Main(string[] args)
{
      string s ="";
      //(1)字符访问(下标访问s[i])
      s ="ABCD";
      Console.WriteLine(s[0]); // 输出"A";
      Console.WriteLine(s.Length); // 输出4
      Console.WriteLine();
      //(2)打散为字符数组(ToCharArray)
      s ="ABCD";
      char[] arr = s.ToCharArray(); // 把字符串打散成字符数组{'A','B','C','D'}
      Console.WriteLine(arr[0]); // 输出数组的第一个元素,输出"A"
      Console.WriteLine();
      //(3)截取子串(Substring)
      s ="ABCD";
Console.WriteLine(s.Substring(1)); // 从第2位开始(索引从0开始)截取一直到字符串结束,输出"BCD"
      Console.WriteLine(s.Substring(1, 2)); // 从第2位开始截取2位,输出"BC"
      Console.WriteLine();
      //(4)匹配索引(IndexOf())
      s ="ABCABCD";
      Console.WriteLine(s.IndexOf('A')); // 从字符串头部开始搜索第一个匹配字符A的位置索引,输出"0"
      Console.WriteLine(s.IndexOf("BCD")); // 从字符串头部开始搜索第一个匹配字符串BCD的位置,输出"4"
      Console.WriteLine(s.LastIndexOf('C')); // 从字符串尾部开始搜索第一个匹配字符C的位置,输出"5"
      Console.WriteLine(s.LastIndexOf("AB")); // 从字符串尾部开始搜索第一个匹配字符串BCD的位置,输出"3"
      Console.WriteLine(s.IndexOf('E')); // 从字符串头部开始搜索第一个匹配字符串E的位置,没有匹配输出"-1";
      Console.WriteLine(s.Contains("ABCD")); // 判断字符串中是否存在另一个字符串"ABCD",输出true
      Console.WriteLine();
      //(5)大小写转换(ToUpper和ToLower)
      s ="aBcD";
      Console.WriteLine(s.ToLower()); // 转化为小写,输出"abcd"
      Console.WriteLine(s.ToUpper()); // 转化为大写,输出"ABCD"
      Console.WriteLine();
      //(6)填充对齐(PadLeft和PadRight)
      s ="ABCD";
      Console.WriteLine(s.PadLeft(6, '_')); // 使用'_'填充字符串左部,使它扩充到6位总长度,输出"__ABCD"
      Console.WriteLine(s.PadRight(6, '_')); // 使用'_'填充字符串右部,使它扩充到6位总长度,输出"ABCD__"
      Console.WriteLine();
      //(7)截头去尾(Trim)
      s ="__AB__CD__";
      Console.WriteLine(s.Trim('_')); // 移除字符串中头部和尾部的'_'字符,输出"AB__CD"
      Console.WriteLine(s.TrimStart('_')); // 移除字符串中头部的'_'字符,输出"AB__CD__"
      Console.WriteLine(s.TrimEnd('_')); // 移除字符串中尾部的'_'字符,输出"__AB__CD"
      Console.WriteLine();
      //(8)插入和删除(Insert和Remove)
      s ="ADEF";
      Console.WriteLine(s.Insert(1, "BC")); // 在字符串的第2位处插入字符串"BC",输出"ABCDEF"
      Console.WriteLine(s);
      Console.WriteLine(s.Remove(1)); // 从字符串的第2位开始到最后的字符都删除,输出"A"
      Console.WriteLine(s);
      Console.WriteLine(s.Remove(0, 2)); // 从字符串的第1位开始删除2个字符,输出"EF"
      Console.WriteLine();
      //(9)替换字符(串)(Replace)
      s ="A_B_C_D";
      Console.WriteLine(s.Replace('_', '-')); // 把字符串中的'_'字符替换为'-',输出"A-B-C-D"
      Console.WriteLine(s.Replace("_", "")); // 把字符串中的"_"替换为空字符串,输出"A B C D"
      Console.WriteLine();
      //(10)分割为字符串数组(Split)——互逆操作:联合一个字符串静态方法Join(seperator,arr[])
      s ="AA,BB,CC,DD";
      string[] arr1 = s.Split(','); // 以','字符对字符串进行分割,返回字符串数组
      Console.WriteLine(arr1[0]); // 输出"AA"
      Console.WriteLine(arr1[1]); // 输出"BB"
      Console.WriteLine(arr1[2]); // 输出"CC"
      Console.WriteLine(arr1[3]); // 输出"DD"
      Console.WriteLine();
      s ="AA--BB--CC--DD";
      string[] arr2 = s.Replace("--", "-").Split('-'); // 以字符串进行分割的技巧:先把字符串"--"替换为单个字符"-",然后以'-'字符对字符串进行分割,返回字符串数组
      Console.WriteLine(arr2[0]); // 输出"AA"
      Console.WriteLine(arr2[1]); // 输出"BB"
      Console.WriteLine(arr2[2]); // 输出"CC"
      Console.WriteLine(arr2[3]); // 输出"DD"
      Console.WriteLine();
      //(11)格式化(静态方法Format)
      Console.WriteLine(string.Format("{0} + {1} = {2}", 1, 2, 1+2));
      Console.WriteLine(string.Format("{0} / {1} = {2:0.000}", 1, 3, 1.00/3.00));
      Console.WriteLine(string.Format("{0:yyyy年MM月dd日}", DateTime.Now));
      //(12)连接成一个字符串(静态方法Concat、静态方法Join和实例方法StringBuilder.Append)
      s ="A,B,C,D";
      string[] arr3 = s.Split(','); // arr = {"A","B","C","D"}
      Console.WriteLine(string.Concat(arr3)); // 将一个字符串数组连接成一个字符串,输出"ABCD"
      Console.WriteLine(string.Join(",", arr3)); // 以","作为分割符号将一个字符串数组连接成一个字符串,输出"A,B,C,D"
      StringBuilder sb =new StringBuilder(); // 声明一个字符串构造器实例
      sb.Append("A"); // 使用字符串构造器连接字符串能获得更高的性能
      sb.Append('B');
      Console.WriteLine(sb.ToString());// 输出"AB"
      Console.ReadKey();
    }

The above is the detailed content of Summary of methods for operating strings in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么将16进制字符串转为数字php怎么将16进制字符串转为数字Oct 26, 2021 pm 06:36 PM

php将16进制字符串转为数字的方法:1、使用hexdec()函数,语法“hexdec(十六进制字符串)”;2、使用base_convert()函数,语法“bindec(十六进制字符串, 16, 10)”。

php怎么将字符串转换成小数php怎么将字符串转换成小数Mar 22, 2023 pm 03:22 PM

PHP 是一门功能强大的编程语言,广泛应用于 Web 开发领域。其中一个非常常见的情况是需要将字符串转换为小数。这在进行数据处理的时候非常有用。在本文中,我们将介绍如何在 PHP 中将字符串转换为小数。

golang怎么检测变量是否为字符串golang怎么检测变量是否为字符串Jan 06, 2023 pm 12:41 PM

检测变量是否为字符串的方法:1、利用​“%T”格式化标识,语法“fmt.Printf("variable count=%v is of type %T \n", count, count)”;2、利用reflect.TypeOf(),语法“reflect.TypeOf(变量)”;3、利用reflect.ValueOf().Kind()检测;4、使用类型断言,可以对类型进行分组。

分享几个.NET开源的AI和LLM相关项目框架分享几个.NET开源的AI和LLM相关项目框架May 06, 2024 pm 04:43 PM

当今人工智能(AI)技术的发展如火如荼,它们在各个领域都展现出了巨大的潜力和影响力。今天大姚给大家分享4个.NET开源的AI模型LLM相关的项目框架,希望能为大家提供一些参考。https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel是一种开源的软件开发工具包(SDK),旨在将大型语言模型(LLM)如OpenAI、Azure

C#的就业前景如何C#的就业前景如何Oct 19, 2023 am 11:02 AM

无论您是初学者还是有经验的专业人士,掌握C#将为您的职业发展铺平道路。

php 字符串长度不一致怎么办php 字符串长度不一致怎么办Feb 07, 2023 am 09:58 AM

php字符串长度不一致的解决办法:1、通过mb_detect_encoding()函数查看字符串的编码方式;2、通过mb_strlen函数查看具体字符长度;3、使用正则表达式“preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str1, $matches);”剔除非中文字符即可。

go语言怎么删除字符串中的空格go语言怎么删除字符串中的空格Jan 17, 2023 pm 02:31 PM

删除方法:1、使用TrimSpace()函数去除字符串左右两边的空格,语法“strings.TrimSpace(str)”;2、使用Trim()函数去除字符串左右两边的空格,语法“strings.Trim(str, " ")”;3、使用Replace()函数去除字符串的全部空格,语法“strings.Replace(str, " ", "", -1)”。

php字符串函数学习:怎么去掉前面的字符php字符串函数学习:怎么去掉前面的字符Mar 20, 2023 pm 02:33 PM

在开发PHP应用程序时,有时我们需要去掉字符串前面的某些特定字符或者字符串。在这种情况下,我们需要使用一些PHP函数来实现这一目标。本文将介绍一些PHP函数,帮助您轻松地去掉字符串前面的字符或字符串。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),