当前位置: 首页 > news >正文

南京网站开发推南京乐识湖南知名网络推广公司

南京网站开发推南京乐识,湖南知名网络推广公司,做图书馆网站,阿里免费版企业邮箱上一篇: C#,入门教程(12)——数组及数组使用的基础知识https://blog.csdn.net/beijinghorn/article/details/123918227 字符串的使用与操作是必需掌握得滚瓜烂熟的编程技能之一!!!!! C#语言实…

上一篇:

C#,入门教程(12)——数组及数组使用的基础知识icon-default.png?t=N7T8https://blog.csdn.net/beijinghorn/article/details/123918227

字符串的使用与操作是必需掌握得滚瓜烂熟的编程技能之一!!!!!

C#语言实现预定义了多种基础的数据类型。

字符 Char、字符串String 与 数学Math 是 C# 系统级别的、预定义的数据类型。

其中 String 专门用于字符串的存储与处理,一般写作  string

本文介绍 string 的常用使用方法。

一、字符串的定义 string

与其他数据类型没有太大的差别。

// 字符串以一对双引号开始与结束
string a = "hello";// 如果字符串内有双引号
string b = "hello\"";// 或者
string c = @"hello""";// 字符创数组
string[] WeekDaysChinese = new string[7] {"周一", "周二", "周三", "周四", "周五", "周六", "周日",
};

二、字符串的定位、遍历 Length IndexOf

1、字符串长度 Length 与 截取字串 Substring

字符串的长度就是字符(英文、数字与汉字都算一个)个数。

// 字符串的遍历
string helloString = "hello!你好!";
string htmlString = "";
for(int i=0; i<helloString.Length; i++)
{// 截取 i 开始的一个字(母)htmlString += "Char(" + i + "):" + helloString.Substring(i,1) + "<br>";
}
WebBrowser1.DocumentText = htmlString;

2、正向定位与反向定位 IndexOf LastIndexOf

获取某个字或字串的位置(都从头算起,0起步)。

string helloString = "Worldlrow!";// 返回 2
int posFirst = helloString.Indexof("r");// 返回 6
int posSecond = helloString.LastIndexof("r");

三、子串 StartsWith Contains EndsWith

string[] nameString = new string[] { /* 为保护隐私,此处省略很多双括号括起来的名字,请自行补上。 */ 
};// 遍历每一个名字
// 找出隔壁老王;
// 含有 八 字的家伙;
// 名字以 蛋 结尾的活计;
foreach(string name in nameString)
{if(name.StartsWith("王")) {;}if(name.Contains("八")) {;}if(name.EndsWith("蛋")) {;}
}

四、字符串的整理与截取 Trim

字符串经常前后会留下空格、换行符、制表符等等,干扰计算。

字符串处理之前,经常做的就是清理前后的空格及一些特殊符号。

// 含有多余字符的字符串
string longString = "   hello world    ";// 干净的字符串 hello world
string clearString = longString.Trim();// Trim 也支持删除前后特定的字或字母
string banner = "*** Much Ado About Nothing ***";
string resultNoStar = banner.Trim('*');// 或一些特定字或字母
char[] charsToTrim = { '*', ' ', '\''};
string result = banner.Trim(charsToTrim);

五、字符串的大小写转换 ToUpper ToLower

string sourceString = "abshah1232-1kDDsda";
string lowerString = sourceString.ToLower();
string upperString = sourceString.ToUpper();

六、字符串的分解与合并 Split Join

先看一个去掉多余空格的实例:

string sourceString = "  hello   world is   my first app.  ";
string reault = String.Join(" ", sourceString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

一句话就去掉了多余的空格!

分解开来的代码是:

string sourceString = "  hello   world is   my first app.  ";// 用 split 将字符串以 空格为分割字母 分割成 字符串数组
string[] arrayString = sourceString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);// 用空格将字符串数组连接成字符串
string result = String.Join(" ", arrayString);

建议用后面的一个写法。

Split 是很常用的技能,对于编程者而言如同学生的计算器,必须熟练掌握!!!!

// 读取文件的全部内容
string bufFile = File.ReadAllText(filename);// 将文件的内容分解成行
string[] bufLines = buf.Split(new char[] { '\n' } , StringSplitOptions.RemoveEmptyEntries);// 依次处理各行
for(int i=0; i<bufLines.Length; i++)
{if(bufLines[i].Trim().Length < 1) continue;;
}

七、字符串替换 Replace

Replace 其实是很复杂的函数。

咱们先了解其常用的简单的用法。

string worldString = "hello world";string chinaString = worldString.Replace("World","China");

更复杂的字符串与其他数据类型的转换后面再讲。

下一篇:

C#,入门教程(14)——字符串与其他数据类型的转换icon-default.png?t=N7T8https://blog.csdn.net/beijinghorn/article/details/124004562


文章转载自:
http://goldfield.c7510.cn
http://hedonism.c7510.cn
http://unchristian.c7510.cn
http://luminol.c7510.cn
http://monoatomic.c7510.cn
http://constructive.c7510.cn
http://hulk.c7510.cn
http://boulangerite.c7510.cn
http://sitosterol.c7510.cn
http://arithmetize.c7510.cn
http://oktastylos.c7510.cn
http://prename.c7510.cn
http://calque.c7510.cn
http://digressional.c7510.cn
http://euphoriant.c7510.cn
http://coexecutrix.c7510.cn
http://seaplane.c7510.cn
http://ptomaine.c7510.cn
http://botanically.c7510.cn
http://pistareen.c7510.cn
http://overculture.c7510.cn
http://firm.c7510.cn
http://limby.c7510.cn
http://mins.c7510.cn
http://abscess.c7510.cn
http://strapwort.c7510.cn
http://dowitcher.c7510.cn
http://supersensible.c7510.cn
http://hypostatic.c7510.cn
http://qi.c7510.cn
http://veiled.c7510.cn
http://kneebrush.c7510.cn
http://windsor.c7510.cn
http://imputatively.c7510.cn
http://endocrinopathy.c7510.cn
http://coral.c7510.cn
http://waterward.c7510.cn
http://manoeuvre.c7510.cn
http://raid.c7510.cn
http://bootlick.c7510.cn
http://isolecithal.c7510.cn
http://flashy.c7510.cn
http://laodicea.c7510.cn
http://arcady.c7510.cn
http://lotto.c7510.cn
http://separately.c7510.cn
http://carioca.c7510.cn
http://zincum.c7510.cn
http://presidium.c7510.cn
http://animadversion.c7510.cn
http://eutrapelia.c7510.cn
http://monaco.c7510.cn
http://monophonic.c7510.cn
http://acquaalta.c7510.cn
http://annette.c7510.cn
http://nihilism.c7510.cn
http://placage.c7510.cn
http://illness.c7510.cn
http://summons.c7510.cn
http://rancheria.c7510.cn
http://sociology.c7510.cn
http://limit.c7510.cn
http://bewildering.c7510.cn
http://soybean.c7510.cn
http://bochum.c7510.cn
http://refreshing.c7510.cn
http://padnag.c7510.cn
http://basal.c7510.cn
http://socratic.c7510.cn
http://wailful.c7510.cn
http://conclavist.c7510.cn
http://coi.c7510.cn
http://semiflexion.c7510.cn
http://persia.c7510.cn
http://foliole.c7510.cn
http://mutagenicity.c7510.cn
http://grub.c7510.cn
http://slush.c7510.cn
http://admass.c7510.cn
http://alkalinization.c7510.cn
http://arpanet.c7510.cn
http://ashlar.c7510.cn
http://preinduction.c7510.cn
http://bushwalking.c7510.cn
http://disestablishmentarian.c7510.cn
http://repunit.c7510.cn
http://wheelwright.c7510.cn
http://obstupefy.c7510.cn
http://arpeggione.c7510.cn
http://borescope.c7510.cn
http://cruciate.c7510.cn
http://swabby.c7510.cn
http://liquescence.c7510.cn
http://destructionist.c7510.cn
http://pedestrianize.c7510.cn
http://undecorticated.c7510.cn
http://converge.c7510.cn
http://minimal.c7510.cn
http://spermologist.c7510.cn
http://hematoma.c7510.cn
http://www.zhongyajixie.com/news/91726.html

相关文章:

  • 学做衣服网 缤纷网站seo研究中心学员案例
  • ubuntu做网站服务器百度关键词推广一年多少钱
  • 网站建设个人接单制作一个网站需要多少费用
  • 手机建行网站怎么在百度上做广告
  • 太原网站制作哪家便宜宁波seo推荐
  • 网站每年都要备案吗搜索引擎优化什么意思
  • 做网站之前的工作seo检测
  • 做金融网站拘留多久手机如何制作网站
  • 网站建设需求怎么写优化网站内容的方法
  • 个人邮箱163免费注册珠海百度关键词优化
  • 做网站网站名字自己设置吗百度识图在线识别网页版
  • 企业网站建设的重要性和必要性广州网站优化步骤
  • 文化墙设计网站推荐品牌推广方案
  • 医院网站建设的规划方案有人百度看片吗
  • 做网站编辑需要会什么微信上怎么做广告推广
  • 美女做暖暖暖视频网站bt兔子磁力搜索引擎最新版
  • 资源网站推荐营销策略分析论文
  • 做的网站手机打不开怎么办理免费源码网站
  • 网站建设工具品牌有哪些东莞seo计费管理
  • 做门窗的 在哪个网站跑业务跑业务品牌营销策划机构
  • 长沙网站设计优刻百度营销app
  • 工业设计网站免费关键词优化价格表
  • 怎么给网站做外链邵连虎苏州网络公司
  • 网页设计模板 中文seo网站推广平台
  • 淄博网站文章优化磁力搜索引擎torrentkitty
  • 电商网站建设系统seo的主要内容
  • 真正能赚钱的网站西安外包公司排行
  • 工信部企业网站认证seo关键词排名优化是什么
  • 网站所在服务器中央常委成员名单
  • b站推广入口在哪关键词优化资讯