在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# 字符与字符串操作的更多相关文章

  1. go内建容器-字符和字符串操作

    1.基础定义 在基础语法篇提到过golang的rune相当于其他编程语言的char,其本质是一个int32(四字节),用[]rune来转换一个字符串时,得到的是个解码后的结果,存储在新开辟的[]run ...

  2. C 语言函数手册:涵盖字符测试、字符串操作、内存管理、时间换算、数学计算、文件操作、进程管理、文件权限控制、信号处理、接口处理、环境变量、终端控制

    1. 字符测试函数 函数 说明 isascii() 判断字符是否为ASCII码字符 2. 字符串操作 函数 说明 gcvt() 将浮点型数转换为字符串(四舍五入) index() 查找字符串并返回首次 ...

  3. 有关UNICODE、ANSI字符集和相关字符串操作

    Q UNICODE字符串如何显示 A 如果程序定义了_UNICODE宏直接用 WCHAR *str=L"unicodestring"; TextOut(0,0,str); 否则就需 ...

  4. ABAP字符串操作 截取字符长度 取位数

    ABAP字符串操作   ABAP對字串的操作方法與其他語言的操作有較大差別,以下是較常用的對字串操作的方法: 1. 字串的連接:CONCATENATEDATA: t1 TYPE c LENGTH 10 ...

  5. day4-基础 字符串操作,文件操作,字符转编码

    1.字符串用法 name = 'daniel' print(name.capitalize()) #首字母大写 >>>daniel print(name.count('a')) #统 ...

  6. ES6字符串操作讲解(详细),字符串编码表,代码单元,码点的详细介绍。

    以前用到字符串的方法时候,并不会深刻的去思考其中的原理,所以在es6新增的这些方法里就有点蒙圈了,于是想要搞清楚为什么会新增这些方法,以及如何使用这些方法. 在博客园上看见一篇大神SamWeb的总结, ...

  7. python基础之 列表、元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码

    本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...

  8. Android(Java) 字符串的常用操作,获取指定字符出现的次数,根据指定字符截取字符串

    /*这是第100000份数据,要截取出100000*/ String s="这是第100000份数据"; String s1 = s.substring(s.indexOf(&qu ...

  9. js-DOM ~ 05. Date日期的相关操作、string、查字符串的位置、给索引查字符、字符串截取slice/substr/substring、去除空格、替换、大小写、Math函数、事件绑定、this

    内置对象:  语言自带的对象/提供了常用的.基本的功能 打印数组和字符串不用for... in   /   打印josn的时候采用for...in Date 获取当前事件:   var date = ...

  10. Core Java 总结(字符和字符串类问题)

    所有代码均在本地编译运行测试,环境为 Windows7 32位机器 + eclipse Mars.2 Release (4.5.2) 2016-10-17 整理 字符,字符串类问题 正则表达式问题 J ...

随机推荐

  1. C++ lambda 内 std::move 失效问题的思考

    最近在学习 C++ Move 时,有看到这样一个代码需求:在 lambda 中,将一个捕获参数 move 给另外一个变量. 看似一个很简单常规的操作,然而这个 move 动作却没有生效. 具体代码如下 ...

  2. JSP | application 对象详解 & 网站计数器实现

    原作者为 RioTian@cnblogs, 本作品采用 CC 4.0 BY 进行许可,转载请注明出处. 本篇学习自:C语言中文网,部分内容转载仅供学习使用. \[QAQ \] JSP applicat ...

  3. AcWing 第五场周赛

    比赛链接:Here AcWing 3726. 调整数组 签到题 void solve() { int n; cin >> n; int x = 0, y = 1, c; for (int ...

  4. AtCoder Beginner Contest 187 题解

    A - Large Digits 按要求求出两个数的每位之和,进行比较即可. 时间复杂度 \(\mathcal{O}(\log(AB))\). B - Gentle Pairs 枚举所有点对求斜率. ...

  5. @Conditional注解使用及@ConditionalOnXXX各注解的作用

    本文为博主原创,转载请注明 出处: 一.@Conditional注解作用: 必须是 @Conditional 注解指定的条件成立,才会在容器中添加组件,配置类里面的所有配置才会生效 二.@Condit ...

  6. The requested URL could not be retrieved

    在开发过程中,调用对外接口,返回了一长串的标签提示,如下 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "ht ...

  7. HashMap集合遍历随机性问题分析

    一.原因分析 1.1 HashMap对象的遍历 HashMap的遍历是通过此类中字段table数组进行顺序遍历,原因如下所示: 1 #HashMap 迭代遍历源码 2 public final boo ...

  8. 使用pip安装pycharm插件时,要使用管理员权限打开cmd安装

    1.问题 安装到一半报错 报错1 报错2 2.解决 解决1 原文:https://blog.csdn.net/weixin_44899752/article/details/128372969 下面是 ...

  9. [转帖]【sql server安全】sql server连接加密,sql server SSL加密连接

    https://www.cnblogs.com/gered/p/13595098.html#_label1_0 MSSQL - 最佳实践 - 使用SSL加密连接 回到顶部 author: 风移 回到顶 ...

  10. Kubeadm 安装支持IPV6 K8S1.28.x的简单过程

    Kubeadm 安装支持IPV6 K8S的简单过程 背景 手贱 找了一个晚上想尝试安装一个K8S集群 并且可以支持IPV6 协议栈的 然后就开始各种百度. 各种处理 找到了一堆歪门邪道. 但是还不知道 ...