【C#(五)基础篇—字符串】教程文章相关的互联网学习教程文章

C# List<string>转换成字符串 按指定的字符分隔【代码】

var stringExcept = result.Except(compareList).ToList(); //两个List获取差集 string notExistValue = String.Join(",", stringExcept.ToArray());//数组转换成string用逗号分隔转自: https://www.cnblogs.com/huangtailang/p/4106314.html

C#计算字符串中的汉字个数【代码】

C#使用正则表达式计算字符串中的汉字个数。 /// /// 计算字符串中的汉字个数 /// ///字符串 /// 汉字个数 public static int getHanZiCount(string str) {Regex r = new Regex("^[\u4E00-\u9FA5]{0,}$");int count = 0; // 汉字个数for (int i = 0; i < str.Length; i++){if (r.IsMatch(str[i].ToString())){count++;}}return count; }

C# 截取两个指定字符串中间的字符串列表【代码】

/// <summary>/// 截取两个指定字符串中间的字符串列表(开始和结束两个字符串不能相同!)/// </summary>/// <param name="content"></param>/// <param name="startStr"></param>/// <param name="endStr"></param>/// <returns></returns>public static List<string> GetAllSubstring(string content, string startStr, string endStr){List<string> resultList = new List<string>();int len = content.Length;int startLen = st...

C# 字符串转枚举

private void btnStart_Click(object sender, EventArgs e){ string testText = "Sunday"; Days2 d = (Days2)Enum.Parse(typeof(Days2), testText); MessageBox.Show(d.ToString());} enum Days2{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}

C#字符串中数字提取

参考文献 https://www.cnblogs.com/cuihongyu3503319/p/10429739.html 在进行数字提取时请添加引用using System.Text.RegularExpressions; 否则编译出错。 string str = "提取123.11abc提取"; //我们抓取当前字符当中的123.11 str=Regex.Replace(str, @"[^\d.\d]", ""); // 如果是数字,则转换为decimal类型 if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$")) { decimal result = decimal.Parse(str); Console.WriteLine("使...

c# 【MVC】WebApi返回各种类型(图片/json数据/字符串)

using System.IO;/// <summary> /// WebApi返回图片 /// </summary> public HttpResponseMessage GetQrCode() {var imgPath = @"D:\ITdosCom\Images\itdos.jpg";//从图片中读取bytevar imgByte = File.ReadAllBytes(imgPath);//从图片中读取流var imgStream = new MemoryStream(File.ReadAllBytes(imgPath));var resp = new HttpResponseMessage(HttpStatusCode.OK){Content = new ByteArrayContent(imgByte)//或者//Content = new...

C# 生成随机密码(随机字符串)的代码【代码】

把做工程过程中较好的内容段做个收藏,下面的内容是关于C# 生成随机密码(随机字符串)的内容,应该能对各位朋友有些帮助。private static int getNewSeed(){byte[] rndBytes = new byte[4];System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();rng.GetBytes(rndBytes);return BitConverter.ToInt32(rndBytes, 0); static public string GetRandomString(i...

C#基础-字符串

字符串比较,strA.CompareTo(strB) A大于B 正数 A小于B 负数 A等于B 0 string strA = "ab"; string strB = "jk"; int intResult = strA.CompareTo(strB); Console.WriteLine(intResult); 查找字符串的位置 IndexOf,LastIndexOf,StartWith,EndWith string str = "Hello world!"; int index = str.IndexOf('o'); int index = str.LastIndexOf('o'); int index = str.IndexOf("lo"); Console.WriteLine(index); 格式化输出 string str...

【C#冷知识系列】(二)关于字符串的冷知识【图】

字符串忽略转义符string?i?=?"sss\ssss";string?i?=?@"sss\ssss";输出:sss"sssssss\ssss模板字符串string?j?=?$"i?is?{i}";?//那么i的值就会被插入到字符串中。这里不仅要知道可以这么用,更要知道为什么可以这么用:模板字符串或者叫插值字符串只是String.Format的一个语法糖,编译器会这样解析:string?s1?=?"Hello";string?s2?=?String.Format("Hello,?{0}",?s1);注意:如果想输出花括号,就写两个大括号 string?s1?=?"Hello";...

c# 正则表达式替换字符串中常见的特殊字符【代码】

第一种,若字符串中含有字母,则使用以下方法 public static string RemoveSpecialCharacterToupper(string hexData){//下文中的‘\\’表示转义return Regex.Replace(hexData, "[ \\[ \\] \\^ \\-_*――(^)|$%~!@#$…&%¥—+=<>《》!!???::?`、。,;,.;\"‘’“”-]", "").ToUpper();}其他: public static string RemoveSpecialCharacter(string hexData){//下文中的‘\\’表示转义return Regex.Replace(hexData, "[ \\[ \\...

C#通过编辑距离计算两个字符串的相似度的代码【代码】

将开发过程中较好的一些代码段备份一下,下面的代码是关于C#通过编辑距离计算两个字符串的相似度的代码,应该能对码农们有些帮助。 using System; using System.Text.RegularExpressions; using System.Threading.Tasks;namespace Levenshtein {public delegate void AnalyzerCompletedHander(double sim);public class LevenshteinDistance:IDisposable{private string str1;private string str2;private int[,] index;int k;Task...

超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文长度为2)【图】

正常情况下,我们是直接去string的length的,但是汉字是有两个字节的,所以直接用length是错的。如下图:所以应该用以下代码来获取长度: ?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27private void button1_Click(object sender, EventArgs e) ????{ ??????string s = textBox1.Text; ??????int i = GetLength(s); ??????MessageBox.Show(i.ToString()); ????} ????public static int GetLength(s...

c# 格式化字符串

字符说明示例输出C 货币 string.Format("{0:C3}", 2) $2.000D 十进制 string.Format("{0:D3}", 2) 002E 科学计数法 1.20E+001 1.20E+001G 常规 string.Format("{0:G}", 2) 2N 用分号隔开的数字 string.Format("{0:N}", 250000) 250,000.00X 十六进制 string.Format("{0:X000}", 12) C

C# -- 内插字符串的使用【代码】【图】

C# -- 内插字符串的使用 (1) 字符串文本以 $ 字符开头,后接左双引号字符。 $ 符号和引号字符之间不能有空格。(2) 内插字符串表达式的结果可以是任何数据类型。(3) 可通过在内插表达式后接冒号(“:”)和格式字符串来指定格式字符串。 static void Main(string[] args){var name = "小胡子";var age = 26;var email = "xiaohuzi@xiaohuzi.com";var salary = 3700.21;var today = DateTime.Now;Console.WriteLine($"我叫{na...

[NOI.AC#34]palinedrome 字符串hash+贪心

容易看出,只要从两边往中间扫描,碰到相等的就直接分割然后加入答案即可,判断相等用字符串hash #include<bits/stdc++.h> #define REP(i,a,b) for(int i(a);i<=(b);++i) #define dbg(...) fprintf(stderr,__VA_ARGS__) using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; template<typename T,typename U>inline char smin(T&x,const U&y){return x>y?x=y,1:0;} template<ty...