C#字符串基础
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#字符串基础,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2650字,纯文字阅读大概需要4分钟。
内容图文
static void Main(string[] args)
{
//How to run C# in VS Code?
/*
step 0: create a null folder and open it in vscode
step 1: dotnet new console
step 2: dotnet restore
step 3: dotnet run
*/
Console.WriteLine("Hello World!");
////////////////////////////////////
//Common string is unchangable
string str = "hello//:www.world.edu.uk";
int result = str.CompareTo("hello");//return 0 when 2 strings are the same
result = string.Compare(str, "hello");//same as above
Console.WriteLine(result);//0
result = str.CompareTo("hallo");//return 1 when the first different letter-index in string is lager
result = string.Compare(str, "hallo");//same as above
Console.WriteLine(result);//1
result = str.CompareTo("hillo");//return -1 when the first different letter-index in string is smaller
result = string.Compare(str, "hillo");//same as above
Console.WriteLine(result);//-1
string newStr = str.Replace("ll", "ff");//replacement won‘t change original string
Console.WriteLine($"Original:{str}. New:{newStr}.");//original string is still the originale one
string targetString = "world";
int indexOfTargetString = str.IndexOf(targetString);//returns the index of the first letter in target string
Console.WriteLine($"index of {targetString} is {indexOfTargetString}");//12
indexOfTargetString = str.IndexOf(targetString + "X");//return -1 if no target string was found
Console.WriteLine($"index of {targetString + "X"} is {indexOfTargetString}");//-1
//Use them in comprohensive way such as below:
if (!str.IndexOf(targetString).Equals(-1))
{
string m_targetString = str.Substring(str.IndexOf(targetString), targetString.Length);
Console.WriteLine($"my target string from the sentence: {m_targetString}");
}
////////////////////////////////////
//Common string is unchangable and may cause wastes
//We use string builder to create strings which often changes
StringBuilder stringBuilder = new StringBuilder("Hello World!");
StringBuilder stringBuilder2 = new StringBuilder(100);//open string space size of 100, space will be extended if exceeds
StringBuilder stringBuilder3 = new StringBuilder("Hello World!", 100);//initialize and open 100 soze of string space, space will be extended if exceeds
Console.WriteLine(stringBuilder);
stringBuilder.Append("---By Alexander");//Effective
Console.WriteLine(stringBuilder);
stringBuilder.Insert(5, "*a lamp inserted*");
Console.WriteLine(stringBuilder);
stringBuilder.Replace("Hello", "Conia sayo");//This will affect the original string
Console.WriteLine(stringBuilder);//original string has been changed
stringBuilder.Clear();
Console.WriteLine(stringBuilder);
}
原文:https://www.cnblogs.com/ezhar/p/12862150.html
内容总结
以上是互联网集市为您收集整理的C#字符串基础全部内容,希望文章能够帮你解决C#字符串基础所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。