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 ...
随机推荐
- AtCoder Regular Contest 120 AB题
比赛链接:Here A - Max Add 观察一下发现每次输出与两点有关,前缀和和当前位置最大值 int main() { cin.tie(nullptr)->sync_with_stdio( ...
- vue学习笔记 一、环境搭建
系列导航 vue学习笔记 一.环境搭建 vue学习笔记 二.环境搭建+项目创建 vue学习笔记 三.文件和目录结构 vue学习笔记 四.定义组件(组件基本结构) vue学习笔记 五.创建子组件实例 v ...
- uniapp#实现自定义省市区三级联动
uni-APP中的三级联动(省市区)---数据前端写死 https://blog.csdn.net/lwaner/article/details/107150805 uniapp#实现自定义省市区三级 ...
- token原理分析
- es6-10
- 13个构建RESTful API的最佳实践
前言 Facebook.GitHub.Google和其他许多巨头都需要一种方法来服务和消费数据.在今天的开发环境中,RESTful API仍然是服务和消费数据的最佳选择之一. 但你是否考虑过学习行业标 ...
- python3之lambda表达式
技术背景 lambda表达式本身是一个非常基础的python函数语法,其基本功能跟使用def所定义的python函数是一样的,只是lambda表达式基本在一行以内就完整的表达了整个函数的运算逻辑.这里 ...
- APB
APB Usage APB主要用于寄存器和外设(GPIO\TIMER\UART)的访问 CPU 通过AXI接口发出transaction访问外设,AXI-based interconnect接收到AX ...
- Makeflie脚本使用
1.目标 2.Makefile的作用 自动化编译仿真 文件有引用层级关系,Tb会引用RTL顶层,RTL顶层也会引用一些其他的小的模块,编译的时候被引用的文件需要先进行编译. 脚本有两种模式,debug ...
- SpringMVC07——Ajax
Ajax AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. AJA ...