c#中string的一些基本用法
.string的Split方法的使用 这个例子就是通过制定的符号来将词组分开,Splite(分割的字符,分割的份数) using System;
using System.Collections; public class Test
{
public static void Main()
{
string data = "Mike,McMillan,3000 W. Scenic,North Little Rock,AR,72118";
string[] sdata;
char[] delimiter=new char[]{','};
sdata = data.Split(delimiter,);
foreach (string val in sdata)
{
Console.WriteLine(val);
}
}
} .string的Join用法,使用指定的连接符来对字符数组进行连接
using System;
using System.Collections;
using System.Linq; public class Test
{
public static void Main()
{
string [] sdata=new string[]{"i","want","to","do","it"};
string data;
data=String.Join(" ", sdata);
Console.WriteLine(data); }
} .string的其他方法:
Euqal:用于比较两个字符串的大小,如果相等就返回True,如果不相等就放回false; Compare To:比较两个字符串的大小,如果根据大小返回-,,; .StartsWith和EndsWith的用法
这个函数用来判断字符当中是否是以指定字符开始或者结束的
using System;
using System.Collections;
using System.Linq; public class Test
{
public static void Main()
{
string []strs=new string[]{"dogs","cats","mat","apples","banana"};
foreach (string val in strs)
{
if(val.EndsWith("s"))
Console.WriteLine(val);
if(val.StartsWith("a"))
Console.WriteLine("**"+val);
} }
} .字符的插入Insert,Remove
可以在指定位置插入一个字符,并返回处理过的字符串
Remove可以在指定位置删除指定长度的字符
using System;
using System.Collections;
using System.Linq; public class Test
{
public static void Main()
{
string str = "你好,今天真好";
str = str.Insert(, "啊");
Console.WriteLine(str);
str = str.Remove(, );
Console.WriteLine(str);
}
} .Replace方法
该方法是用于替换字符串中的字符
using System;
using System.Collections;
using System.Linq; public class Test
{
public static void Main()
{
string str = "你好啊,我已经完成了";
str = str.Replace("好", "不");
Console.WriteLine(str); }
} .文本对齐方式
using System;
using System.Collections;
using System.Linq;
public class Test
{
public static void Main()
{
string s1 = "hello";
string s2 = "world";
string s3 = "goodbyte";
Console.WriteLine(s1.PadLeft(10)); //用于左对齐(空格补齐左对齐)
Console.WriteLine(s2.PadRight(10)); //用于右对齐 }
} .字符串的大小写转换
using System;
using System.Collections;
using System.Linq; public class Test
{
public static void Main()
{
string s1 = "hello";
s1 = s1.ToUpper(); //转化成大写字符
Console.WriteLine(s1); string s2 = "HelL0"; //转换成小写
s2 = s2.ToLower();
Console.WriteLine(s2); }
} .去掉字符串中头部或则尾部的一些其他指定字符,Trim名为修剪,就是修饰用的
该方法只能去掉字符串的头部或则尾部中间的部分不能去掉
using System;
using System.Collections;
using System.Linq; public class Test
{
public static void Main()
{
string[] htmlComments = new string[]
{
"<!-- Start Page !!Number Function -->",
"<!-- Get user name and password-->",
"<!-- End Title page -->",
"<!-- End script -->"
}; char[] commentChars=new char[]{'<','!','-','>'};
for (int i = ; i <=htmlComments.GetUpperBound(); i++)
{
htmlComments[i] = htmlComments[i].Trim(commentChars); //将两端都去掉
//htmlComments[i] = htmlComments[i].TrimEnd(commentChars); //去掉尾部
//htmlComments[i] = htmlComments[i].TrimStart(commentChars); //去掉头部
}
for (int i = ; i <= htmlComments.GetUpperBound(); i++)
{
Console.WriteLine(htmlComments[i]);
} }
}
c#中string的一些基本用法的更多相关文章
- C++中string类的基本用法
#include <iostream> #include <set> using namespace std; int main() { string line; getlin ...
- Java中String类型的部分用法
1.如何将字符串转换为整型数值? int i = Integer.parseInt("20"); 2.如何用“==”还是equals比较两个字符串? “==”是用来比较俩引用是不是 ...
- c++中string.erase()函数的用法(转)
erase函数的原型如下:(1)string& erase ( size_t pos = 0, size_t n = npos );(2)iterator erase ( iterator p ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- 标准C++中string类的用法
转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...
- C#中string.Format 用法详解
这篇文章主要介绍了C#中string.format用法,以实例形式较为详细的讲述了string.format格式化的各种用法,非常具有实用价值,需要的朋友可以参考下 本文实例总结了C#中string. ...
- Java用代码演示String类中的以下方法的用法
用代码演示String类中的以下方法的用法 (1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true (2)char charAt(int index): 返回索引上 ...
- 关于java中String的用法
在java 中String存在许多的基本函数,接下来了解一下这些函数的基本用法 String.equals用法(这个用法比较难) String类中的equals()方法: public boolean ...
- java成神之——java中string的用法
java中String的用法 String基本用法 String分割 String拼接 String截取 String换行符和format格式化 String反转字符串和去除空白字符 String获取 ...
随机推荐
- [再寄小读者之数学篇](2014-06-21 Beal-Kaot-Majda type logarithmic Sobolev inequality)
For $f\in H^s(\bbR^3)$ with $s>\cfrac{3}{2}$, we have $$\bex \sen{f}_{L^\infty}\leq C\sex{1+\sen{ ...
- C++中数组作为形参进行传递(转)
有两种传递方法,一种是function(int a[]); 另一种是function(int *a) 这两种两种方法在函数中对数组参数的修改都会影响到实参本身的值! 对于第一种,根据之前所学,形参是实 ...
- radio日志sim卡信号状态分析
logcat -b radio日志 // 接着将slot 0主卡置为false,将slot 1设置为true 08-09 11:24:40.335 2565 3243 D RILJ : [4820]& ...
- 【Java编程思想笔记】反射
文章参考:学习网站 how2java.cn 参考博客:(敬业的小码哥)https://blog.csdn.net/sinat_38259539/article/details/71799078 (青色 ...
- pythonのsqlalchemy多对多关系
现在来设计一个能描述“图书”与“作者”的关系的表结构,需求是 一本书可以有好几个作者一起出版 一个作者可以写好几本书 #!/usr/bin/env python from sqlalchemy imp ...
- [转载]关于在Linux下上传代码至Github
刚开始使用Github没多长时间,所以很多地方不太熟练,看到阿里云上有一篇文章写得不错,故转载过来. 转载自:https://www.aliyun.com/jiaocheng/122729.html ...
- 【原创】大数据基础之Spark(7)spark读取文件split过程(即RDD分区数量)
spark 2.1.1 spark初始化rdd的时候,需要读取文件,通常是hdfs文件,在读文件的时候可以指定最小partition数量,这里只是建议的数量,实际可能比这个要大(比如文件特别多或者特别 ...
- 小程序获取formid配置模板消息
小程序无限获取formid,发送模板信息 1.发送模板信息需要条件:formid 2.formid产生环境:提交form表单产生,并且只有真机才能出现————安卓一个13位的时间戳(近期使用得时候,安 ...
- win10+caffe+GPU
由于学习需要,决定安装caffe,之前用的都是基于theano的keras.听说win下caffe很难配置,经过一个下午和晚上的配置终于成功,以此记录. 我的电脑:win10 64位,N卡GTX950 ...
- Emacs Org-mode 2 文档结构
2.1 章节 org-mode用* 标识章节,一个* 代表一级标题,两个* 代表两级标题,以此类推.最多6颗星,也就是最多6级. 书写格式如下: * 标题一 ** 标题二 注意, * 后有空格.不同的 ...