1.Compare 比较字符串

用来比较2个字符串的长度大小和值是否相同,相同则返回0,当x比y小返回-1,否则返回1,如果长度相同,且值不同,则返回1,代码如下
public static void Main()
{
string x = "nihao";
string y = "nihao ma";结果:-1
//2.string x = "nihao ma";
//string y = "nihao";结果: 1
//3.string x = "nihao";
//string y = "nihao";结果: 0
//4.string x = "niliu";
//string y = "nihao";结果: 1
int result = string.Compare(x,y);
Console.WriteLine("结果:{0}",result);
Console.ReadKey();
}
2.String.CompareOrdinal 比较字符(还没有搞清楚,欢迎各位补充)
通过计算每个字符串中相应 System.Char 对象的数值来比较两个指定的 System.String 对象
public static int CompareOrdinal(string strA, string strB); 
 
public static int CompareOrdinal(string strA, int indexA, string strB, int indexB, int length); 
通过计算每个字符串中相应 System.Char 对象的数值来比较两个指定的 System.String 对象  
 
3.Concat 连接字符串
public static string Concat(paramsstring[] values);
用来连接多个字符串实例
string x="你好";
string y=",欢迎你";
string z=string.Concat(x,y);
结果是:你好,欢迎你
Concat和+的不同:
Concat只能用来连接字符串,同时是方法名。
+号是运算符,可以连接多种类型。
 
4.CopyTo
public void CopyTo(拷贝intsourceIndex,char[] destination,intdestinationIndex,intcount);
这个方法理解起来有点难度,只是找到下面的例子
string dest = "Hello world";
string source = "Goodbye China";
char[] destArray = dest.ToCharArray();//将dest变成字符数组
source.CopyTo(8, destArray, 6, 5);//从source的第8个字符起复制5个字符并从destArray的第6个位置开始放
dest = new string(destArray);//这时dest为"Hello China"
Console.WriteLine(dest);
 

输出结果是:Hello China

 
5. IndexOf 索引字符的位置
定位字符
  • intIndexOf(charvalue)
  • intIndexOf(charvalue,intstartIndex)
  • intIndexOf(charvalue,intstartIndex,intcount)

定位字符串:

  1. intIndexOf(stringvalue)
  2. intIndexOf(stringvalue,intstartIndex)
  3. intIndexOf(stringvalue,intstartIndex,intcount)

在上述重载形式中,其参数含义如下:

Value:待定位的字符或者子串。

startIndex:在总串中开始搜索的起始位置。

Count:在总串中从起始位置开始搜索的字符数。

1)一个参数,索引字符第一次出现的位置,返回int 
例子:
String str1 = "hello world";
String str2 = "abcd";
int x = str1.IndexOf("o");
Console.WriteLine("结果是{0}",x);
结果是4,也就是说o在字符串str1中的位置是4.
indexof(string str,int i)
2)二个参数,表示从i+1的位置开始索引。
String str1 = "hello world";
String str2 = "abcd";
int x = str1.IndexOf("o");
int y = str1.IndexOf("o",5);
Console.WriteLine("结果是{0},定索引位置的索引结果是{1}",x,y);
Console.ReadKey();
结果是:7
定位从第5个开始搜索,搜索到o的位置是7,返回int 是7
3)三个参数,确定开始和要查的几位数的位置
indexof(string str,int i,int j)
String str1 = "hello world or happy you ";
int z = str1.IndexOf("o",10,4);
Console.WriteLine("倒序索引结果{0}",z);
从第10位开始查找,查询2位,结果是12
 
6.LastIndexOf 索引最后的位置
定位最后一次的位置,如果是三个参数,就是从后往前索引。
 
7.IndexOfAny
表示索引字符数组中,最近的一个值的位置。
String str1 = "hello world or happy you ";
char[] b = { 'e', 'o', 'l' };
int a = str1.IndexOfAny(b,5,15);
Console.WriteLine("结果是{0}}",a);
返回int为7
 
8.Insert 插入字符串
public string Insert(intstartIndex,stringvalue);
把一个字符串插入到另一个字符串的指定位置
String str1 = "hello world or happy you ";
String str2 = "abcd";
string str3 = str1.Insert(2, str2);
Console.WriteLine("insert功能插入结果{0}",str3);
这里是吧str2插入到str1中。插入位置2得到结果heabcdllo world or happy you
 
9.Join
在字符串数组中插入指定的字符
str5必须是字符串数组
 
string[] str5 = {"fsa","fasdf","fsaf"};
string str4 = string.Join("/", str5);
Console.WriteLine("Join功能结果{0}", str4);
输出结果fsa/fasdf/fsaf
 
 
10.PadLeft和PadRight
PadLeft是指在左面插入指定字符串总长度的char类型
string str1 = "hello world";
char str2='a';
string str3 = str1.PadLeft(12,str2);
Console.WriteLine("PadLeft功能插入结果{0}", str3);
结果是:ahello world
PadRight是指在右面插入指定字符串总长度的char类型
string str1 = "hello world";
char str2='a';
string str3 = str1.PadRight(12,str2);
Console.WriteLine("PadLeft功能插入结果{0}", str3);
结果是:hello worlda
 
11.Replace 替换字符串

public string Replace(char oldChar, char newChar);

public string Replace(string oldValue, string newValue);

string str1 = "hello world";
str1 = str1.Replace("d", "d!");
Console.WriteLine("Replace功能插入结果{0}", str1);
str1要更改的字符串,第一个参数是要更改的字符,第二个参数更新后的字符
结果是:hello world!
 
12.Split 分隔字符串
public string[] Split(paramschar[] separator);
string str1 = "hello world";
string[] str2 = str1.Split('w');
 结果是:hello world! 字符串数组,需要用for遍历获取,第一个参数是old,第二个参数是new
如果想获取分割后的单个值,可以这样使用
string str1 = "hello world";
string str2 = str1.Split('w')[0];
Console.WriteLine("分割数据{0}",str2);
这样就相当于获取数组中第一个值hello,如果是[1],那么获取的就是orld。
 
13.remove移除指定位置字符串
 public string Remove(int startIndex); 
移除的位置从sartIndex到结束位置的字符串
 public string Remove(int startIndex, int count); 
移除startIndex位置到count位置的字符串
string str1 = "changed";
string str4 = str1.Remove(1, 2);
str4得到的是cnged,移除了第二位和第三位。
 
14.Substring这个方法用的比较多(截取字符串)
String.Substring (Int32)         从此实例检索子字符串。子字符串从指定的字符位置开始。
string s = "Hello C# World!";
string s1=s.Substring(3);
Console.WriteLine(s1);
结果是:lo C# World!    //s1为从s中截取的位置为3的字符以后的字符子串,表示从第4位开始截取字符。
 
String.Substring (Int32, Int32) 从此实例检索子字符串。子字符串从指定的字符位置开始且具有指定的长度。
string s = "Hello C# World!";
string s1=s.Substring(3,2);
Console.WriteLine(s1);
结果是lo,后面一个参数限制了截取的位数。
 
 
 
 
 
 
 

C#基础知识学习(2)string类中的方法的更多相关文章

  1. String类中intern方法的原理分析

    一,前言 ​ 昨天简单整理了JVM内存分配和String类常用方法,遇到了String中的intern()方法.本来想一并总结起来,但是intern方法还涉及到JDK版本的问题,内容也相对较多,所以今 ...

  2. 2019.4.1今日一练String类中的方法

    package com.pjc.objects;                        replaceAll()方法的理解引出正则表达式import java.util.regex.Patte ...

  3. (Object String 类中的方法练习)

    package com.zs.demo1; public class Demo1 { public static void main(String[] args) { fun1(); fun2(); ...

  4. 【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!

    split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator],[limit])参数:stringObj   必选项.要被分解的 ...

  5. String类中toCharArray()方法的用法

    该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符 eg:  public class class6_3 { public static void main(String arg ...

  6. C#基础知识系列三(类和结构体、String和StringBuilder、equals和==)

    前言 这一节主要来了解一下类和结构体之间的异同点.以及针对String和StringBuilder的用法.equals和==,其实可以看出很多地方都用到了上一节的值类型和引用类型.堆栈和装箱拆箱操作吧 ...

  7. String类中的常用方法

    String类 一.转换成String方法 1.public String(); 空参构造 初始化一个新创建的 String 对象,使其表示一个空字符序列 2.public String(byte[] ...

  8. Java基础知识强化101:Java 中的 String对象真的不可变吗 ?

    1. 什么是不可变对象?       众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...

  9. Java基础知识强化之集合框架笔记33:Arrays工具类中asList()方法的使用

    1. Arrays工具类中asList()方法的使用 public static <T> List<T> asList(T... a): 把数组转成集合 注意事项: 虽然可以把 ...

随机推荐

  1. Nest.js你学不会系列-初识Nest

    前言 最近在学习研究 Nest 框架,但是在学习过程中除了参考翻阅官方文档外国内几乎没有多少资料能系统的讲解 Nest 的相关内容,所以打算想通过我自己学习的角度讲解下 Nest 框架,不知道能坚持多 ...

  2. es8对object快速遍历的方法

    let grade = { 'lilei' : 96, 'han' : 99 } //遍历keys console.log(Object.keys(grade)) console.log(Object ...

  3. 机器学习环境配置系列三之Anaconda

    1.下载Anaconda文件 进入anaconda的官网 选择对应的系统 选择希望下载的版本(本人下载的是Anaconda 5.3 For Linux Installer Python 3.7 ver ...

  4. CF990G

    题意 https://codeforces.com/contest/990/problem/G 思考 在200000以内,因数个数最多的数位166320,共有160个因数.可以知道,从一个节点向下走最 ...

  5. 了解人工智能?-百度AI

    了解人工智能? 什么是人工智能? 由人创造的"智慧能力",同样具备智慧生物的能力 耳朵=倾听=麦克风=语音识别 ASR Automatic Speech Recognition 嘴 ...

  6. 利用geojson实现模型轨迹运动

    直接上代码 var viewer = new Cesium.Viewer('cesiumContainer'); //Set the random number seed for consistent ...

  7. Git详解之分布式应用

    前言 为了便于项目中的所有开发者分享代码,我们准备好了一台服务器存放远程 Git 仓库.经过前面几章的学习,我们已经学会了一些基本的本地工作流程中所需用到的命令.接下来,我们要学习下如何利用 Git ...

  8. yield 伪并发例子

    import timedef custumer(name): print('%s 准备吃饺子了'%name) while True: curry = yield print('饺子%s来了 ,被%s吃 ...

  9. K8S搭建教程及部署脚本

    部署环境: 主机名 IP地址 系统OS 内核 master 10.5.1.10 CentOS7 Linux master 3.10.0-1062 node1 10.5.1.11 CentOS7 Lin ...

  10. ROS中的3D建模机器人(三)

    一.利用xacro理解机器人建模 当我们创建复杂的机器人模型时,URDF的灵活性将会降低,URDF缺少的主要特性是简单的.可重用性,模块化和可编程性. URDF是一个单独的文件我们不能在它里面包含其他 ...