char 支持的方法

字符串
声明字符串
String str = [null];  可以用此方法声明一个空字符串

 

 
连接字符串
str +"" + str1;

  

比较两个字符串
Compare 静态方法 返回int
比较两个字符串是否相等,最常用的2个重载方法
Int Compare(string a,string b)
Int Compare(string a,string b ,bool ignorCase) 第三方参数是true 忽略大小写
String.Compare("aaa","bbb");

  

 
CompareTo 返回int
  和compare差不多 不过他可以以实例对象本身与字符串对比
int flag = str1.CompareTo("aab");

  

 
Equals 比较两个字符串 成功返回 true 失败返回 false
string a = "aab";
string b = "aac";
bool c = a.Equals(b);
Console.WriteLine(c);

  

 
Format 格式化字符串
string newstr = String.Format("aaa{0} bbb {1}","1","2");
Console.WriteLine(newstr);

  

 
Substring(start,end) 截取字符串
String a = "abcdefg";
String b = ""; //我测试的 b字符串必须预先定义
b = a.Substring(1,4);

  

 
split 分割字符串成数组
String a = "eeeabcdebcde";
char[] sep = {'a','c'};
String[] b = a.Split(sep);
for (int i = 0; i < sep.Length; i++) {
  Console.WriteLine("item {0} {1}",i,b[i]);
}

  

 
Insert 字符串中插入
String a = "abcdefg";
string b;
b = a.Insert(3,"hahaha");

  

 
 
PadLeft PadRight 字符串填充
  Public String PadRight(int totalWidth,char c);
    totalWidth 填充后字符串的长度
    c 要填充的 char 类型字符
String a = "abcdefg";
string b;
a = a.PadRight(9,'z');

  

 
Remove 删除指定位置字符串
String a = "abcdefg";
a = a.Remove(3,2); //开始位置 从开始位置删除几个

  

 
Copy 复制字符串
String a = "abc";
String b;
b = String.Copy(a);

  

 
CopyTo 和 copy差不多 不过可以复制字符串一部分到另一个数组里
Public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);
sourceIndex 需要复制的字符串起始位置
destination 目标字符数组
destinationIndex 制定目标数组中的开始存放位置
count 指定要复制的字符个数
例子:
String st1 = "用一生下载你";
char[] str = new char[100];
st1.CopyTo(1,str,0,4); //将字符串st从索引1开始的4个字符串复制到字符数组str中
异常:ArgumentOutOfRangeException
sourceIndex、destinationIndex 或 count 为负
count 大于从 startIndex 到此实例末尾的子字符串的长度
count 大于从 destinationIndex 到 destination 末尾的子数组的长度

  

 
Replace 替换字符串
public string Replace(char OChar,char NChar); 主要用于替换字符串中的字符
public string Replace(string Ovalue,string NValue); 主要用于替换字符串中的字符串
ochar 待替换的字符
nchar 替换后的新字符
ovalue 待替换的字符串
nvalue 替换后的新字符串
String str1 = "用一生下载你";
string b = str1.Replace('一','二'); 替换字符
string c = str1.Replace("用一生","去你妈的"); 替换字符串

  

c# Char && string的更多相关文章

  1. const char* && string && String^ 类型转换

    const char* && string && String^ 类型转换 const char* ---> string const char * cw= &q ...

  2. const char* <----- > string

    (1) const char*      <-----     string const char* const_txt_path=txt_path.c_str(); (2)  string  ...

  3. c#中 uint--byte[]--char[]--string相互转换汇总

    原文:c#中 uint--byte[]--char[]--string相互转换汇总 在在做一些互操作的时候往往需要一些类型的相互转换,比如用c#访问win32api的时候往往需要向api中传入DWOR ...

  4. 【C语言】reverse_string(char * string)(递归)

    递归reverse_string(char * string)性能. 逆转 原始字符串 更改 相反,打印出的. /* 编写一个函数reverse_string(char * string)(递归实现) ...

  5. MFC中char*,string和CString之间的转换

    MFC中char*,string和CString之间的转换 一.    将CString类转换成char*(LPSTR)类型 方法一,使用强制转换.例如:  CString theString( &q ...

  6. CString转char * ,string

    CString头文件#include <afx.h> string头文件#include <string.h> 1.CString转char * CString cstr; c ...

  7. wchar_t char string wstring 之间的转换

    wchar_t char string wstring 之间的转换 转:http://blog.csdn.net/lbd2008/article/details/8333583 在处理中文时有时需要进 ...

  8. VS2013 MFC C++ CString ,const char , char, string 类型转换

    VS2013 测试 以下测试加入头文件: # include <string>#include <cstdlib>using namespace std; //-------- ...

  9. c++编写递归函数char *itostr (int n,char *string),该函数将整数n转换为十进制表示的字符串。

    #include<iostream> #include<stdio.h> using namespace std; ; char *itostr (int n,char *St ...

  10. System::String *,char*,string 等的类型转换 [转]

    在VC 的编程中,经常会用到各种类型的转换,在MFC中textbox等控件得到的返回类型是System::String *,而写入的文件要求是 const char *类型的,下面介绍一些转换的方法: ...

随机推荐

  1. windows c++程序移植到linux的要点

    这段时间得到一份源码,是Windows下的,调试了一把,可以正常运行,可是没有Linux版本,而实际的应用场景是要在Linux服务器上面运行 所以涉及到Windows下c++程序的移植,有同事竭力推荐 ...

  2. JavaScript call()和apply()

    ECMAScript规范给所有函数都定义了call()与apply()两个方法,call()与apply()的第一个参数都是需要调用的函数对象,在函数体内这个参数就是this的值,剩余的参数是需要传递 ...

  3. 学习笔记 - 用js判断页面是否加载完成实现代码

    用document.onreadystatechange的方法来监听状态改变, 然后用document.readyState == "complete"判断是否加载完成 docum ...

  4. bzoj:1941: [Sdoi2010]Hide and Seek

    1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec  Memory Limit: 162 MBSubmit: 531  Solved: 295[Submi ...

  5. BZOJ 1509: [NOI2003]逃学的小孩

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1509 直接求出树的直径,枚举每个点更新一遍答案. #include<cstring> ...

  6. hbmy周赛1--C

    C - Exam Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  7. hbmy周赛1--A

    Age Sort You are given the ages (in years) of all people of a country with at least 1 year of age. Y ...

  8. bat复制文件夹下所有文件到另一个目录

    一个需求,网上了半天都是错了,所以记一下吧,方便你我. copy是文件拷贝,文件夹拷贝需要用到xcopy @echo off::当前盘符set curPath=%cd%set digPath =&qu ...

  9. union 时只能查出一个表中的信息,另一个表只能查出字段

    原因:news表中title字段的编码,与brand表中的编码不一致导致 y

  10. Screen命令安装使用教程

    在安装lnmp之前,我们一般先运行一下Screen程序,因为screen好像一个容器一样,把lnmp的安装过程保护了起来.以CentOS中安装lnmp为例,程序下载.编译都需要比较长的时间,如果中途遇 ...