c#中的String方法
1.Replace(替换字符):
public string Replace(char oldChar,char newChar);在对象中寻找oldChar,如果寻找到,就用newChar将oldChar替换掉。
如:
string st = "abcdef";
string newstring = st.Replace('a', 'x');
Console.WriteLine(newstring); //即:xbcdef
public string Replace(string oldString,string newString);在对象中寻找oldString,如果寻找到,就用newString将oldString替换掉。
如:
string st = "abcdef";
string newstring = st.Replace("abc", "xyz");
Console.WriteLine(newstring); //即:xyzdef
2.Remove(删除字符):
public string Remove(int startIndex);从startIndex位置开始,删除此位置后所有的字符(包括当前位置所指定的字符)。
如:
string st = "abcdef";
string newstring = st.Remove(4);
Console.WriteLine(newstring); //即:abcd
public string Remove(int startIndex,int count);从startIndex位置开始,删除count个字符。
如:
string st = "abcdef";
string newstring = st.Remove(4,1);
Console.WriteLine(newstring); //即:abcdf
3.Substring(字符串提取):
public string Substring(int startIndex);从startIndex位置开始,提取此位置后所有的字符(包括当前位置所指定的字符)。
如:
string st = "abcdef";
string newstring = st.Substring(2);
Console.WriteLine(newstring); //即:cdef
public string Substring(int startIndex,int count);从startIndex位置开始,提取count个字符。
如:
string st = "abcdef";
string newstring = st.Substring(2,2);
Console.WriteLine(newstring); //即:cd
4.Trim(清空空格):
public string Trim ():将字符串对象包含的字符串两边的空格去掉后返回。
public string Trim ( params char[] trimChars ):从此实例的开始和末尾移除数组中指定的一组字符的所有匹配项。
如:
string st ="abcdef";
string newstring = st.Trim(new char[] {'a'});//寻找st字符串中开始与末尾是否有与'a'匹配,如有,将其移除。
Console.WriteLine(newstring); //即:bcdef
注:如果字符串为"aaaabcdef",返回依然为bcdef。当移除第一个a时,开始依然为a,继续移除,直到没有。
public string TrimEnd ( params char[] trimChars ):对此实例末尾与指定字符进行匹配,true则移除
public string TrimStart ( params char[] trimChars ):对此实例开始与指定字符进行匹配,true则移除
5.ToLower(转换大小写)
public string ToLower():将字符串对象包含的字符串中的大写全部转换为小写。
6.IndexOf(获取指定的字符串的开始索引)
public int IndexOf (sring field):在此实例中寻找field,如果寻找到,返回开始索引,反之,返回-1。
如:
string st = "abcdef";
int num=st.IndexOf("bcd");
Console.WriteLine(num); //即:1
7.Equals(是否相等)
public bool Equals (string value):比较调用方法的字符串对象包含字符串和参数给出的对象是否相同,如相同,就返回true,反之,返回false。
如: string a = "abcdef";
bool b = a.Equals("bcdef");
Console.WriteLine(b);//即:false
public bool Equals ( string value, StringComparison comparisonType ):比较调用方法的字符串对象包含字符串和参数给出的对象是否在不区分大小写的情况下相同,如相同,就返回true,反之,返回false,第二个参数将指定区域性、大小写以及比较所用的排序规则.
如:
string a = "ABCDEF";
bool b = a.Equals("abcdef",StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine(b);//即:true
8.Split(拆分)
public string[] Split ( params char[] separator ):根据separator 指定的没有字符分隔此实例中子字符串成为Unicode字符数组, separator可以是不包含分隔符的空数组或空引用。
public string[] Split ( char[] separator, int count ):参数count 指定要返回的子字符串的最大数量。
如:
string st = "语文|数学|英语|物理";
string[] split = st.Split(new char[]{'|'},2);
for (int i = 0; i < split.Length; i++)
{
Console.WriteLine(split[i]);
}
注:count不填则全部拆分
public enum StringSplitOptions
成员名称 说明
None 返回值包括含有空字符串的数组元素
RemoveEmptyEntries 返回值不包括含有空字符串的数组元素
如:
string st = "语文|数学||英语|物理";
string[] split = st.Split(new char[]{'|'},StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < split.Length; i++)
{
Console.WriteLine(split[i]);
}
将StringSplitOptions枚举和Split()方法联系起来:
1. public string[] Split ( char[] separator, StringSplitOptions options ):options指定StringSplitOptions枚举的RemoveEmptyEntries以省略返回的数组中的空数组元素,或指定StringSplitOptions枚举的None以包含返回的数组中的空数组元
2. public string[] Split ( char[] separator, int count, StringSplitOptions options )
3. public string[] Split ( string[] separator, StringSplitOptions options )
4. public string[] Split ( string[] separator, int count, StringSplitOptions options )
9.Contains(判断是否存在)
public bool Contains(string text):如果字符串中出现text,则返回true,反之false,如果text为("")也返回true。
如:
string st="语文数学英语";
bool b=st.Contains("语文");
Console.WriteLine(b);//true
10.EndsWith,StartsWith(判断字符串的开始或结束)
public bool EndsWith ( string value ):判断对象包含字符串是否以value指定的字符串结束,是则为 true;否则为 false。
public bool EndsWith ( string value, StringComparison comparisonType ):第二个参数设置比较时区域、大小写和排序规则。
public bool StartsWith ( string value ):判断对象包含字符串是否以value指定的字符串开始,是则为 true;否则为 false。
public bool StartsWith ( string value, StringComparison comparisonType ) :第二个参数设置比较时区域、大小写和排序规则。
如:
string st="语文数学英语abc";
bool b=st.EndsWith("英语ABC",StringComparison.CurrentCultureIgnoreCase);//第二个参数忽略大小比较。
Console.WriteLine(b);//true
11.Insert(字符串插入)
public string Insert ( int startIndex, string value ):在指定的字符串下标为startIndex前插入字符串value。返回插入后的值。
c#中的String方法的更多相关文章
- JavaScript 中的string 方法
创建string的方法 var str ="abc"; var str = new String("abc"); var str = String(" ...
- php示例代码之类似于C#中的String.Format方法
php示例代码之类似于C#中的String.Format方法 原文来自于 http://stackoverflow.com/questions/1241177/c-string-format-equ ...
- Java String类中的intern()方法
今天在看一本书的时候注意到一个String的intern()方法,平常没用过,只是见过这个方法,也没去仔细看过这个方法.所以今天看了一下.个人觉得给String类中加入这个方法可能是为了提升一点点性能 ...
- String对象中常用的方法
String对象中常用的方法 1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...
- String中intern的方法
首先查看官方API那个的解释: ——————————————————————————————————————— intern public String intern() 返回字符串对象的规范化表示形 ...
- java面向对象中的String类中12种常用的方法
1.字符串与字符数组的转换 字符串可以使用toCharArray()方法变成一个字符数组,也可以使用String类的构造方法把一个字符数组变成一个字符串. public class StringAPI ...
- Java 字符串比较,String 中的一些方法 == 和 equals 的详解
"==" 是比较的是两个对象的内存地址,而equals方法默认情况下是比较两个对象的内存地址. 1.String str = "hello" 生成的字符串,首 ...
- 详解String类中的intern()方法
我们用一个经典的例子来理解 package com.jvm.heap; public class MyTest { public static void main(String[] args) { S ...
- String方法,js中Array方法,ES5新增Array方法,以及jQuery中Array方法
相关阅读:https://blog.csdn.net/u013185654/article/details/78498393 相关阅读:https://www.cnblogs.com/huangyin ...
随机推荐
- c/c++学习系列之内存对齐
1.C++内存对齐规则 每个特定平台上的编译器都有自己的默认“对齐系数”(也叫对齐模数).程序员可以通过预编译命令#pragma pack(n),n=1,2,4,8,16来改变这一系数,其中的n就是你 ...
- 梳理一下我理解的aop
在看了很多网上的资料和记录之后,我大概捋了下SpringAOP的各种阶段: 基本的advice编程,利用ProxyFactory拿代理类 利用spring把ProxyFactory,advice等be ...
- 100 Same Tree 相同的树
给定两个二叉树,写一个函数来检查它们是否相同.如果两棵树在结构上相同并且节点具有相同的值,则认为它们是相同的.示例 1:输入 : 1 1 / \ ...
- python转换已转义的字符串
python转换已转义的字符串 有时我们可能会获取得以下这样的字符串: >>> a = '{\\"name\\":\\"michael\\"} ...
- Python相对导入导致SystemError的解决方案(译)
原文出处: http://stackoverflow.com/ 译文出处:yibohu1899 这个问题是如何解决在相对导入的时候,如果出现’System Error’的时候的解决方案.顺带一提, ...
- JDK原子类操作
JDK原子类操作及原理 在JDK5之后,JDK提供了对变量的原子类操作, java.util.concurrent.atomic里都是原子类 原子类的分类 原子更新基本类型 原子更新数组 原子更新抽象 ...
- js中函数的理解
在JavaScript中,函数是一个极容易引起误解或引发歧义的数据类型,它可以是独立的函数类型,又可以作为对象的方法,也可以被称为类或构造器,还可以作为函数对象而存在等.
- Sencha Touch和jQuery Mobile的比较
第一组-行销和平台支持 Sencha Touch和jQuery Mobile都以HTML5框架著称.jQuery Mobile谦虚的说自己只是内建于所有流行的移动设备平台,而Sencha Touch则 ...
- 解决Unsupported major.minor version 51.0报错问题
问题产生原因:计算机环境变量的jdk版本与eclipse使用的jdk版本不一致 解决方法: 1.查看计算机环境变量的jdk版本 2.查看eclipse项目java compiler的方法:在项目点右键 ...
- https增加临时证书,tomcat配置
1Windows下: 1.1 生成keystore文件及导出证书 打开控制台: 运行: %JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RS ...