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

C#数据类型:string简单使用

string cmdstr = "Helloworld!";
cmdstr = cmdstr.Remove(cmdstr.Length - 1);//删除最后一个字符

//字符串连接
string str1 = "Hello";
string str2 = "World";
string result1 = str1 + " " + str2;
string result2 = $"{str1} {str2}";//建议使用干方式连接字符

//字符串长度
string str = "Hello";
int length = str.Length;

//字符串插入
string str = "HelloWorld";
string result = str.Insert(5, ", ");
Console.WriteLine(result); // 输出: Hello, World

//字符串删除
string str = "Hello, World!";
string result = str.Remove(5, 2);
Console.WriteLine(result); // 输出: Hello World!

//字符串复制
string str = "Hello";
string copy = string.Copy(str);
Console.WriteLine(copy); // 输出: Hello

//字符串格式化日期
DateTime date = DateTime.Now;
string formattedDate = date.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine(formattedDate); // 输出: 当前日期和时间的格式化字符串

//字符串比较
string str1 = "Hello";
string str2 = "hello";
bool isEqual = string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);

//字符串查找
string str = "Hello, World!";
int index = str.IndexOf("World");

//字符串替换
string str = "Hello, World!";
string result = str.Replace("World", "C#");

//字符串分割
string str = "Hello,World,C#";
string[] parts = str.Split(',');
foreach (var part in parts)
{
Console.WriteLine(part); // 输出: Hello, World, C#
}

//字符串截取
string str = "Hello, World!";
string result = str.Substring(7, 5);
Console.WriteLine(result); // 输出: World

//字符串转小写
string str = "HELLO, WORLD!";
string lowerCase = str.ToLower();
Console.WriteLine(lowerCase); // 输出: hello, world!

//字符串转大写
string str = "hello, world!";
string upperCase = str.ToUpper();
Console.WriteLine(upperCase); // 输出: HELLO, WORLD!

//字符串修剪
string str = "   Hello, World!   ";
string trimmed = str.Trim();
Console.WriteLine(trimmed); // 输出: Hello, World!

//字符串格式化
string name = "Alice";
int age = 30;
string result = string.Format("Name: {0}, Age: {1}", name, age);
Console.WriteLine(result); // 输出: Name: Alice, Age: 30

//字符串拼接
List<string> words = new List<string> { "Hello", "World", "C#" };
string result = string.Join(", ", words);
Console.WriteLine(result); // 输出: Hello, World, C#

//字符串是否为空或空字符串
string str1 = "";
string str2 = null;
bool isNullOrEmpty1 = string.IsNullOrEmpty(str1);
bool isNullOrEmpty2 = string.IsNullOrEmpty(str2);
Console.WriteLine(isNullOrEmpty1); // 输出: True
Console.IsNullOrWhiteSpace(isNullOrEmpty2); // 输出: True

//字符串编码转换
string str = "Hello, 世界!";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
string result = System.Text.Encoding.UTF8.GetString(bytes);
Console.WriteLine(result); // 输出: Hello, 世界!

//字符串反转
string str = "Hello";
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
string result = new string(charArray);
Console.WriteLine(result); // 输出: olleH

http://www.dtcms.com/a/537920.html

相关文章:

  • 回龙观手机网站建设服务一般通过唾液传染的疾病有哪些
  • 强对流天气定义及其分类
  • 国外著名购物网站排名网站建设页面设计之后
  • 联合建设官方网站腾讯搜索引擎入口
  • 《图解技术体系》Wonderful talk AI ~~ Unified Model
  • 文化传播公司针对会务服务和企业拓展活动服务有哪些知识产权布局方向
  • Linux的进程认识下
  • 网站服务器位于北美搜索引擎优化案例
  • 阿里云服务起做网站.天津网站建设
  • 郑州建设信息网站wordpress 花生壳
  • 网站开发是否属于技术合同wordpress 3 企业主题下载
  • 张家口网站建设价格网页设计模板素材美食
  • 慕课网站建设开题报告兄弟懂的拿走不谢d8s8
  • 让IOT版说话
  • 北京网站制作开发公司免费域名解析平台
  • 为什么简洁网站会受到用户欢迎网站建设实战
  • 如何修改网站爱站seo工具包官网
  • 做网站用多大的画布厦门建设网官方网站
  • 哪里做网站排名海外广告投放是干嘛的
  • 4A架构分析
  • 网站怎么做营销网站推广策略
  • 做网站时数据库要创建几个表长春网站推广
  • 如何快速将普通电脑改造为深度学习工作站
  • 教育考试类网站建设中国新闻社领导名单
  • iis访问网站打开要很久吉安网站建设收费
  • 【Python】Python字符串格式化
  • Kotlin 高级语法复习:sealed class、Flow、inline、crossinline等分析,使用实例
  • 怎么做wood网站教程长沙企业网站
  • 顺德网站制作案例机构天津seo优化公司哪家好
  • 力扣-移动零