当前位置: 移动技术网 > IT编程>开发语言>c# > String类的方法应用

String类的方法应用

2019年10月11日  | 移动技术网IT编程  | 我要评论

string类的几个方法的应用示例:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;

namespace consoleapplication1
{
class program
{
static void main(string[] args)
{
string str1 = "dood morning!";
string str2 = "how are you?";
//输出两个字符串
console.writeline(str1);
console.writeline(str2);
//输出两个字符串的长度
console.writeline("字符串str1的长度为{0}", str1.length);
console.writeline("字符串str2的长度为{0}", str2.length);
//比较字符串的长度
console.writeline(str1.compareto(str2));
//判断字符串是否相等
console.writeline(str1.equals(str2));
//输出和给定字符串在原字符串中的位置
console.writeline("字符串\"are\"在str2中的位置是:{0}", str2.indexof("are"));
//输出和给定字符串在原字符串中最后出现的位置
console.writeline("字符串\"are\"在str2中最后出现出现的位置是:{0}", str2.lastindexof("are"));
//判断字符串是否以给定字符串开始
console.writeline(str1.startswith("good"));
//判断字符串是否以给定字符串结尾
console.writeline(str1.endswith("morning"));
//提取字符串,从第五位开始,提取七个字符,输出新字符串
console.writeline(str1.substring(5, 7));
//在原字符串中从第八位开始,插入给定字符串
console.writeline(str1.insert(8, "hello"));
//从原字符串的第六位开始,删除三个字符
console.writeline(str1.remove(6, 3));
//去掉字符串前后的空格
console.writeline(str1.trim());
console.readline();
}
}
}

运行结果如下:


 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网