C# 字符与字符串操作
在C#中,字符和字符串是两个重要的数据类型,有许多内置的方法可以处理字符和字符串。这些方法是非常有用的,可以帮助开发人员更方便、更高效地处理文本数据。
格式化字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string Str1 = "hello lyshark";
// 取出字符串中指定的字符
char Str2 = Str1[1];
Console.WriteLine("字符: {0} 转大写: {1} 转小写: {2}", Str2, Char.ToUpper(Str2), Char.ToLower(Str2));
Console.WriteLine("是否为数字: {0} 是否为大写: {1} 是否为小写: {2}",
Char.IsNumber(Str2), Char.IsUpper(Str2), Char.IsLower(Str2));
// 将字符串转化为字符数组
char[] chs = Str1.ToCharArray();
for (int x = 0; x < chs.Length - 1; x++)
Console.Write("{0} ", chs[x]);
Console.WriteLine();
// 将字符数组转化为字符串
string Str3 = new string(chs);
Console.WriteLine(Str3);
// 格式化输出字符串
string Str4 = "hello";
string Str5 = "lyshark";
string new_str = String.Format("{0},{1}", Str4, Str5);
Console.WriteLine("格式化后的字符串: {0}", new_str);
Console.ReadKey();
}
}
}
比较字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string Str1 = "hello lyshark";
string Str2 = "hello world";
string Str3 = "hello lyshark";
// Compare 比较字符串,相等返回0不相等返回-1
Console.WriteLine("Str1 比较 Str2 " + String.Compare(Str1, Str2));
Console.WriteLine("Str1 比较 Str3 " + String.Compare(Str1, Str3));
// Compare To 比较字符串
Console.WriteLine("Str1 比较 Str2 " + Str1.CompareTo(Str2));
Console.WriteLine("Str1 比较 Str3 " + Str1.CompareTo(Str3));
// Equals 比较字符串
Console.WriteLine("Str1 比较 Str2 " + Str1.Equals(Str2));
Console.WriteLine("Str1 比较 Str3 " + String.Equals(Str1,Str3));
Console.ReadKey();
}
}
}
截取/分割字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 从第一个位置开始截取3个字符
string Str1 = "hello lyshark";
string Str2 = "";
Str2 = Str1.Substring(1, 3);
Console.WriteLine("截取数据: {0}", Str2);
// 分割字符串变量
string Str3 = "用^一生#下载,百度网盘,资源";
char[] separator = { '^', '#', ',' }; // 定义分割字符
String[] split_string = new String[100];
split_string = Str3.Split(separator);
for (int x = 0; x < split_string.Length;x++ )
{
Console.WriteLine("切割计数: {0} 切割字符串: {1}", x, split_string[x]);
}
// 针对时间的切割方法
string str = "2019-12-12";
char[] chs = { '-' };
string[] date = str.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("{0}年 {1}月 {2}日", date[0], date[1], date[2]);
Console.ReadKey();
}
}
}
插入/删除字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 插入字符串的演示
string Str1 = "下载";
Str1 = Str1.Insert(0, "用一生时间");
Console.WriteLine(Str1);
string Str2;
Str2 = Str1.Insert(Str1.Length, "百度网盘里面的资源");
Console.WriteLine(Str2);
// 填充字符串的演示
string Str3;
Str3 = Str1.PadLeft(Str1.Length + 3, '*'); // 在左侧填充
Console.WriteLine("左侧填充: " + Str3);
Str3 = Str1.PadRight(Str1.Length + 3, '*'); // 在右侧填充
Console.WriteLine("右侧填充: " + Str3);
// 去空格的实现
string str = " hahahah ";
str = str.Trim();
str = str.TrimStart();
str = str.TrimEnd();
// 删除字符串的演示
Console.WriteLine("从索引3处向后删除: " + Str3.Remove(3));
Console.WriteLine("删除指定个数的字符: " + Str3.Remove(1, 3));
Console.ReadKey();
}
}
}
拷贝替换字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 普通的拷贝字符串
string Str1 = "hello lyshark";
string Str2;
Str2 = string.Copy(Str1);
Console.WriteLine("普通拷贝: " + Str2);
// 替换字符串
string Str3 = "one world,one dream";
string Str4 = Str3.Replace(',','*');
Console.WriteLine("将,替换为** => " + Str4);
string Str5 = Str3.Replace("one", "One");
Console.WriteLine("将one替换为One =>" + Str5);
Console.ReadKey();
}
}
}
寻找开头结尾字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "今天天气好晴朗,处处好风光";
// 寻找字符串开头结尾
if (str.StartsWith("今") && str.EndsWith("光"))
Console.WriteLine("ok");
// 从指定字符开始搜索,并返回位置
int index = str.IndexOf("天气", 0);
Console.WriteLine(index);
// 从结束位置开始搜索
string path = @"c:\a\b\c苍\d\e苍\f\g\\fd\fd\fdf\d\vfd\苍老师.wav";
int path_index = path.LastIndexOf("\\");
Console.WriteLine(path_index);
Console.ReadKey();
}
}
}
串联字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Join 将指定字符串使用|串联起来
string name_str = string.Join("|", "张三", "李四", "王五", "赵六", "田七");
Console.WriteLine(name_str);
// 将字符串切割后串联去掉竖线
String[] u_name = { "张三", "李四" ,"王五"};
string ptr = string.Join("|", u_name);
Console.WriteLine("合并后: " + ptr);
string[] strNew = ptr.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("去掉| = " + strNew[1]);
for (int x = 0; x < strNew.Length;x++ )
Console.WriteLine("去掉竖线 [{0}] = {1}",x,strNew[x]);
Console.ReadKey();
}
}
}
字符串倒序输出:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "hello lyshark";
// 实现反转字符串 hello -> olleh
char[] chs = str.ToCharArray();
for (int x = 0; x < chs.Length / 2;x++ )
{
char tmp = chs[x];
chs[x] = chs[chs.Length - 1 - x];
chs[chs.Length - 1 - x] = tmp;
}
str = new string(chs);
Console.WriteLine("反转后的结果: {0}", str);
// 实现反转单词 hello lyshark -> lyshark hello
string str1 = "hello lyshark";
string[] strNew = str1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int x = 0; x < strNew.Length / 2; x++)
{
string tmp = strNew[x];
strNew[x] = strNew[strNew.Length - 1 - x];
strNew[strNew.Length - 1 - x] = tmp;
}
str1 = string.Join(" ", strNew);
Console.WriteLine("反转后的结果: {0}", str1);
Console.ReadKey();
}
}
}
IndexOf搜索字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 通过indexOf 切割特定字符
string email = "admin@blib.cn";
int index = email.IndexOf("@");
string userName = email.Substring(0, index);
string userHost = email.Substring(index + 1);
Console.WriteLine("名字: {0} 主机: {1}",userName,userHost);
// 寻找指定字符出现位置
string str = "abcd wwabcd asdcdsac waascd ascsaaa";
int index1 = str.IndexOf('a');
int count = 1;
while(index1 != -1)
{
count++;
index1 = str.IndexOf('a', index1 + 1);
if (index1 == -1)
break;
Console.WriteLine("第{0}次出现a的位置是{1}", count, index1);
}
Console.ReadKey();
}
}
}
C# 字符与字符串操作的更多相关文章
- go内建容器-字符和字符串操作
1.基础定义 在基础语法篇提到过golang的rune相当于其他编程语言的char,其本质是一个int32(四字节),用[]rune来转换一个字符串时,得到的是个解码后的结果,存储在新开辟的[]run ...
- C 语言函数手册:涵盖字符测试、字符串操作、内存管理、时间换算、数学计算、文件操作、进程管理、文件权限控制、信号处理、接口处理、环境变量、终端控制
1. 字符测试函数 函数 说明 isascii() 判断字符是否为ASCII码字符 2. 字符串操作 函数 说明 gcvt() 将浮点型数转换为字符串(四舍五入) index() 查找字符串并返回首次 ...
- 有关UNICODE、ANSI字符集和相关字符串操作
Q UNICODE字符串如何显示 A 如果程序定义了_UNICODE宏直接用 WCHAR *str=L"unicodestring"; TextOut(0,0,str); 否则就需 ...
- ABAP字符串操作 截取字符长度 取位数
ABAP字符串操作 ABAP對字串的操作方法與其他語言的操作有較大差別,以下是較常用的對字串操作的方法: 1. 字串的連接:CONCATENATEDATA: t1 TYPE c LENGTH 10 ...
- day4-基础 字符串操作,文件操作,字符转编码
1.字符串用法 name = 'daniel' print(name.capitalize()) #首字母大写 >>>daniel print(name.count('a')) #统 ...
- ES6字符串操作讲解(详细),字符串编码表,代码单元,码点的详细介绍。
以前用到字符串的方法时候,并不会深刻的去思考其中的原理,所以在es6新增的这些方法里就有点蒙圈了,于是想要搞清楚为什么会新增这些方法,以及如何使用这些方法. 在博客园上看见一篇大神SamWeb的总结, ...
- python基础之 列表、元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...
- Android(Java) 字符串的常用操作,获取指定字符出现的次数,根据指定字符截取字符串
/*这是第100000份数据,要截取出100000*/ String s="这是第100000份数据"; String s1 = s.substring(s.indexOf(&qu ...
- js-DOM ~ 05. Date日期的相关操作、string、查字符串的位置、给索引查字符、字符串截取slice/substr/substring、去除空格、替换、大小写、Math函数、事件绑定、this
内置对象: 语言自带的对象/提供了常用的.基本的功能 打印数组和字符串不用for... in / 打印josn的时候采用for...in Date 获取当前事件: var date = ...
- Core Java 总结(字符和字符串类问题)
所有代码均在本地编译运行测试,环境为 Windows7 32位机器 + eclipse Mars.2 Release (4.5.2) 2016-10-17 整理 字符,字符串类问题 正则表达式问题 J ...
随机推荐
- Nginx--引用多配置文件
在nginx.conf的http模块,include 指定某个目录下的*.conf user nginx; worker_processes auto; error_log /var/log/ngin ...
- (转)Github+jsDelivr+PicGo 打造稳定快速、高效免费图床
转载自:https://www.itrhx.com/2019/08/01/A27-image-hosting/ 写在开头,之前我是使用Gitee作为图床和Picgo搭配使用的 (图片不允许超过1MB) ...
- Codeforce:455A. Boredom (DP)
https://codeforces.com/problemset/problem/455/A 题意: 给出n个元素,让我们来挑选,如果选了 \(a_k\),获得\(a_k\)点数,同时与\(a_{k ...
- secure boot(三)secure boot的签名和验签方案
简介 FIT 格式支持存储镜像的hash值,并且在加载镜像时会校验hash值.这可以保护镜像免受破坏,但是,它并不能保护镜像不被替换. 而如果对hash值使用私钥签名,在加载镜像时使用公钥验签则可以保 ...
- Qt开发-共享内存使用范例,配合开发者密钥使用后台调试程序或者进入调试模式
共享内存 就之前不是开发了一个Leventure_DeveloperKey用以调试程序嘛,在这里简单聊一下调试模式的方案. 这里的调试分为了两种,一种是调试模式,一种是开发者模式.需要这两种模式的原因 ...
- C# 双向链表的实现
类数据 public class Objects { private int number; /**//* 货物编号 */ private string name; /**//* 货物名称 */ pr ...
- KVM 管理工具:libvirt
libvirt 简介 libvirt 是目前使用最为广泛的对 KVM 虚拟机进行管理的工具和应用程序接口.
- xshell配置隧道转移规则
钢铁知识库,一个学习python爬虫.数据分析的知识库.人生苦短,快用python. xshell是什么 通俗点说就是一款强大ssh远程软件,可以方便运维人员对服务器进行管理操作,功能很多朋友们自行探 ...
- 介绍这个库:C# Blazor中显示Markdown文件
1 讲目的 前几天上线了一个在线Icon转换工具,为了让大家使用放心,改了点代码,在转换下载Icon图标后立即删除临时文件,并在工具下面贴上了工具的开发步骤和代码,大家看这样改是否合适,见Issue ...
- 改变vs私有变量的命名规范
vs默认情况下,private 变量是不带下划线开头的,可以通过设置命名规范,增加下划线开头规则. 点击菜单:[工具]->[选项]->[文本编辑器]->[c#]->[代码样式] ...