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. 本地Git绑定Github仓库

    前言 Window的小伙伴如果还没在本地配好Git环境可以参考:https://www.cnblogs.com/poloyy/p/12185132.html 创建Github仓库 Github绑定本地 ...

  2. playbooks框架部署远程主机

    进入到ansible和python环境 进入python3.6虚拟环境 #su - deploy #source .py3-a2.5-env/bin/activate 加载ansible 2.5版本 ...

  3. kubernetes concepts -- Replication Controller

    Edit This Page ReplicationController NOTE: A Deployment that configures a ReplicaSet is now the reco ...

  4. AVR单片机教程——小结

    本文隶属于AVR单片机教程系列.   第一期挺让我失望的,是我太菜,没有把想讲的都讲出来.经常写了很多,然后一点一点删掉,最后就没多少了. 而且感觉难度不合适,处于很尴尬的位置.讲得简单,难的丢给库, ...

  5. 浏览器警告Failed to decode downloaded font和OTS parsing error: Failed to convert *** font to ***

    昨晚,在做一个自己感兴趣的东西时,发现浏览器报警告,Failed to decode downloaded font以及OTS parsing error: Failed to convert *** ...

  6. 玩转Django2.0---Django笔记建站基础十一(一)(音乐网站开发)

    第十一章 音乐网站开发 本章以音乐网站项目为例,介绍Django在实际项目开发中的应用,该网站共分为6个功能模块分别是:网站首页.歌曲排行榜.歌曲播放.歌曲点评.歌曲搜索和用户管理. 11.1 网站需 ...

  7. LeetCode 858 镜面反射

    题目 有一个特殊的正方形房间,每面墙上都有一面镜子.除西南角以外,每个角落都放有一个接受器,编号为 0, 1,以及 2. 正方形房间的墙壁长度为 p,一束激光从西南角射出,首先会与东墙相遇,入射点到接 ...

  8. FileZilla 报错“the server's certificate is unknown”

    FileZilla 是非常好用的一款FTP SFTP 管理工具. 但是filezilla会报错“the server's certificate is unknown” 并且会在window中看到以下 ...

  9. 优雅写Java之四(类与对象)

    一.类相关用法 二.Bean 三.泛型与注解 四.序列化

  10. Java容器解析系列(17) LruCache详解

    在之前讲LinkedHashMap的时候,我们说起可以用来实现LRU(least recent used)算法,接下来我看一下其中的一个具体实现-----android sdk 中的LruCache. ...