4.1.String类的应用

class String类应用
{
static void Main(string[] args)
{
string astring = "Now is The Time";
//拆分位置
int pos;
//单词
string word;
ArrayList words = new ArrayList();
pos = astring.IndexOf(" ");
while (pos > 0)
{
word = astring.Substring(0, pos);
words.Add(word);
astring = astring.Substring(pos + 1, astring.Length - (pos + 1));
pos = astring.IndexOf(" ");
Console.WriteLine("astring现在的值:" + astring);
}
Console.Read();
}
}

 

4.1.1.Split 方法和 Join 方法

1.Split 方法取得一条字符串后,就会把它分解成数据成分块,然后把这些块放入 String 数组内。

2.Join 方法从数组变为字符串。

string data = "Mike,McMillan,3000 W. Scenic,North Little Rock,AR,72118";
string[] sdata;
char[] delimiter = new char[] { ',' };
sdata = data.Split(delimiter, data.Length);
foreach (string tword in sdata)
Console.WriteLine(tword + " "); string joined;
joined = String.Join(",", sdata);
Console.Write("\n\t"+joined);

 

 

4.1.2.比较字符串

1.第一个要检测的比较方法就是 Equal 方法

string s1 = "foobar";
string s2 = "foobar";
if (s1.Equals(s2))
Console.WriteLine("They are the same.");
else
Console.WriteLine("They are not the same.");

 

2.第二个比较字符串的方法就是 CompareTo

string s1 = "foobar";
string s2 = "foobar"; int s11 = GetASCII(s1); Console.WriteLine(s1.CompareTo(s2)); // 相等 returns 0
s2 = "foofoo";
Console.WriteLine(s1.CompareTo(s2)); //S2低于S1 returns -1
s2 = "fooaar";
Console.WriteLine(s1.CompareTo(s2)); //S2高于S1 returns 1 int compVal = String.Compare(s1, s2);
switch (compVal)
{
case 0: Console.WriteLine(s1 + " " + s2 + " are equal");
break;
case 1: Console.WriteLine(s1 + " is less than " + s2);
break;
case 2: Console.WriteLine(s1 + " is greater than" + s2);
break;
default: Console.WriteLine("Can't compare");
break;
}

 

3.另外两种在处理字符串时会很有用的比较方法是 StartsWith EndsWith

string[] nouns = new string[] { "cat", "dog", "bird", "eggs", "bones" };
ArrayList pluralNouns = new ArrayList();
foreach (string noun in nouns)
if (noun.EndsWith("s"))
pluralNouns.Add(noun);
foreach (string noun in pluralNouns)
Console.Write(noun + " "); Console.WriteLine("\n"); string[] words = new string[] { "triangle", "diagonal", "trimester", "bifocal", "triglycerides" };
ArrayList triWords = new ArrayList();
foreach (string word in words)
if (word.StartsWith("tri"))
triWords.Add(word);
foreach (string word in triWords)
Console.Write(word + " ");

 

 

 

4.1.3.处理字符串的方法

字符串处理通常包括对字符串的改变操作。我们需要在字符串中插入新的字符,从字符串中移除字符,用新字符替换旧字符,改变某些字符的情况,以及向字符串添加空格或者从字符串中移除空格

1.Insert 方法和 Remove 方法

string s1 = "Hello, . Welcome to my class.";
string name = "TangSanSan";
int pos = s1.IndexOf(",");
s1 = s1.Insert(pos + 2, name);
Console.WriteLine(s1);
s1 = s1.Remove(pos + 2, name.Length);
Console.WriteLine(s1);

 

2.Replace 替换

3.PadLeft 方法和 PadRight 方法。 PadLeft 方法会对字符串进行右对齐排列,而 PadRight 方法会对字符串进行左对齐排列。

4.Concat方法。此方法会取走 String对象的列表,把它们串联在一起,然后返回结果字符串。

5.ToLower 方法和 ToUpper 方法还可以把字符串从小写转换成大写形式

6.Trim 方法和 TrimEnd 方法将会把空格或其他字符从字符串的任一端移除掉。

 

 

 

4.2.构造 StringBuilder

在 StringBuilder 类中有几种属性可以用来获取有关 StringBuilder 对象的信息。

Length 属性指定了当前实例中字符的数量,

 Capacity 属性则返回了实例的当前容量。

MaxCapacity 属性会返回对象当前实例中所允许的最大字符数量(尽管这个数量会随着对象添加更多的字符而自动增加)。

1.Append

通过使用 Append 方法可以在 StringBuilder 对象的末尾处添加字符

            StringBuilder stBuff = new StringBuilder();
String[] words = new string[] {"now ", "is ", "the ", "time ", "for ", "all ",
"good ", "men ", "to ", "come ", "to ", "the ","aid ", "of ", "their ", "party"};
for (int i = 0; i <= words.GetUpperBound(0); i++)
stBuff.Append(words[i]);
Console.WriteLine(stBuff);

 

2.AppendFormat

给 StringBuilder 对象添加格式字符串,使用AppendFormat

StringBuilder stBuff = new StringBuilder();
Console.WriteLine();
stBuff.AppendFormat("Your order is for {0} widgets.", 234);
stBuff.AppendFormat("\nWe have {0000} widgets left.", 12);
Console.WriteLine(stBuff);

 

3.Insert

此方法会取得三个参数。第一个参数说明了插入的开始位置。第二个参数则是要插入的字符串。而作为可选项的第三个参数则是一个整数,它用来说明打算在对象中插入字符串的次数。

StringBuilder stBuff = new StringBuilder();
Console.WriteLine();
stBuff.Insert(0, "Hello");
stBuff.Append("world");
stBuff.Insert(5, ", ");
Console.WriteLine(stBuff);
char[] chars = new char[] { 't', 'h', 'e', 'r', 'e' };
stBuff.Insert(5, " " + new string(chars));
Console.WriteLine(stBuff);

 

4.Remove

Remove 方法可以把字符从 StringBuilder 对象中移除掉

 

5.Replace

StringBuilder stBuff = new StringBuilder("HELLO WORLD");
string st = stBuff.ToString();
st = st.ToLower();
st = st.Replace(st.Substring(0, 1),st.Substring(0, 1).ToUpper());
stBuff.Replace(stBuff.ToString(), st);
Console.WriteLine(stBuff);

数据结构和算法 – 4.字符串、 String 类和 StringBuilder 类的更多相关文章

  1. Java学习笔记20(String类应用、StringBuffer类、StringBuilder类)

    1.获取指定字符串中大小写和数字的个数: package demo; public class StringTest { public static void main(String[] args) ...

  2. string类和stringBuilder类

    字符串是C#中的一种重要数据类型,在项目开发中,离不开字符串操作.C#提供了string类实现字符串操作.于Convert类相似,string类中方法有静态方法和非静态方法.注意,在C#中String ...

  3. 一大波Java来袭(四)String类、StringBuilder类、StringBuffer类对照

    本文主要介绍String类.StringBuffer类.StringBuilder类的差别  : 一.概述 (一)String 字符串常量.可是它具有不可变性,就是一旦创建,对它进行的不论什么改动操作 ...

  4. StringBuffer类 和 StringBuilder类

    上一篇中讲解了String类的用法.那么String有什么特点呢? 字符串特点:字符串是常量,其值在创建后就不能被修改.字符串的内容一旦发生变化,就会创建一个新的对象. 代码验证字符串特点: publ ...

  5. Java基础-StringBuffer类与StringBuilder类简介

    Java基础-StringBuffer类与StringBuilder类简介 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.StringBuffer类 在学习过String类之后 ...

  6. StringBuffer类和StringBuilder类

    StringBuffer类和StringBuilder类 三者比较 String 不可变字符序列 底层用char[]存储 StringBuffer 可变的字符序列 线程安全的 效率低 底层结构使用ch ...

  7. Java学习笔记 02 String类、StringBuilder类、字符串格式化和正则表达式

    一.String类一般字符串 声明字符串 >>String str 创建字符串 >>String(char a[])方法用于将一个字符数组创建为String对象 >> ...

  8. java常用类与包装类--常用类字符串String类、StringBuffer类、Stringbuilder类

    1.String类 1.1String类的概念和储存结构: (1)字符串是一个比较特殊的对象,可以使用new,也可以不使用new来创建字符串对象 String s1 = new String(&quo ...

  9. java中String类、StringBuilder类和StringBuffer类详解

    本位转载自http://www.cnblogs.com/dolphin0520/p/3778589.html  版权声明如下: 作者:海子 出处:http://www.cnblogs.com/dolp ...

随机推荐

  1. 剑指Offer 通过中序和先序遍历重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  2. CCF 模拟A 无脑大循环

    http://115.28.138.223:81/view.page?opid=1 第一题用一组STL函数查找即可 #include<iostream> #include<cstdi ...

  3. BZOJ 3665: maths

    Sol 矩阵乘法+快速幂+欧拉定理. 首先观察题目可以发现 \(A_n\) 可以表示成关于 \(K\) 和 \(A_0\) 的几次幂的形式. \(A_0\) 就比较简单了 \(m^n\) 所以第一部分 ...

  4. linux下的/dev/shm目录

    linux下的/dev/shm目录 linux中/dev目录下一般都是一些设备文件,例如磁盘.内存.摄像头等. /dev/shm这个目录是linux下一个利用内存虚拟出来的一个目录,这个目录中的文件都 ...

  5. phpcmsV9.5.8整合百度编辑器Ueditor1.4.3教程

    最近在搞phpcms视频功能,官方的视频功能实在是坑,刚开始是想将优酷的上传功能集成到ckeditor,在coding上有个项目,上传已经集成好了,还没有做上传后视频的获取和显示 项目地址:https ...

  6. HTML页面的布局

    1.纵向切分页面:CSS POSITION的默认值为:STATIC 1) <html> <head> <title>Hello</title> < ...

  7. Print Common Nodes in Two Binary Search Trees

    Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two B ...

  8. FIDO 标准简介

    FIDO 联盟(Fast IDentity Online Alliance)简介 网站:http://fidoalliance.org FIDO Alliance,成立于2012年7月. FIDO的目 ...

  9. poj3341

    AC自动机,用40^4 * 50 * 10的空间进行dp. 最大的难点在于hash. hash一个数列f,数列中的每一位都有一个上限g,即f[i]<=g[i]. 那么可以将该数列hash为这样一 ...

  10. glib-2.49.4 static build step in windows XP

    export LIBFFI_CFLAGS=" -I/usr/local/lib/libffi-3.2.1/include " \ export LIBFFI_LIBS=" ...