String 例子

using System;using System. Collections . Generic ;using System. Linq ;using System. Text ;namesp

using System;

using System. Collections . Generic ;

using System. Linq ;

using System. Text ;

namespace 字符串基础

{

class Program

{

static void Main(string [] args)

{

string [] s = { "a" , "b" , "c" , "d" , "e" }; for (int i = s. Length -1; i >= 0; i--) {

Console. Write ("{0}", s [i ]); }

// output: edcba

string s1 = "hello" ;

char [] chars = s1. ToCharArray (); chars[1] = 'a' ;

string s2 = new string (chars ); Console. WriteLine (s1);

//hello

Console. WriteLine (s2);

//hallo

string s = "Hello" ;

s = s. ToLower ();

Console. WriteLine (s );

// hello

s = s. ToUpper ();

Console. WriteLine (s );

//HELLO

string s1 = " a b ";

s1 = s1. Trim ();

Console. WriteLine ("|{0}|", s1); // |a b|

bool b = ("abc" == "ABC" ); //false bool b = "abc" . Equals ("ABC" ,

StringComparison . OrdinalIgnoreCase );

,

// true

Console. WriteLine (b );

string s1 = "aaa,bbb,ccc.ddd|eee";

string [] strs = s1. Split (',' , '.' , '|');

//Split分隔符

foreach (string item in strs)

{

Console. WriteLine (item );

}

//output:aaa bbb ccc ddd

string s1 = "aa,bb,cc,,dd,3" ;

string [] strs = s1. Split (',' ); // aa bb cc dd 3 string [] strs = s1. Split (new char []{ ',' }, StringSplitOptions . RemoveEmptyEntries );

// aa bb cc dd 3

foreach (var item in strs)

{

Console. WriteLine (item );

}

string s1 = " 我是杰克逊我是麦当娜我是凤姐我是小甜甜" ; string [] strs = s1. Split (new string [] { " 我是" }, StringSplitOptions . RemoveEmptyEntries );

//杰克逊 麦当娜 凤姐 小甜甜

foreach (var item in strs)

{

Console. WriteLine (item );

}

//练习

//i) 从日期字符串“2011-2-6”中分离出年、月和日。 string s1 = "2011-02-06" ;

string [] s = s1. Split ('-' );

for (int i = 0; i < s. Length ; i )

{

Console. WriteLine (s [i ]);

}

string []

lines =System . IO . File . ReadAllLines (@"c:1.txt", Encoding . Default );

,

foreach (var item in lines)

{

Console. WriteLine (item );

}

// Replace() 应用

string s = " 李时珍同志是一名好同志,向李时珍同志学习!" ; s = s. Replace (" 李时珍" , " 李素丽" );

Console. WriteLine (s );

// SbuString() 函数 从指定序号开始一直到字符串的末尾结束 string s = "http://www.google.com";

s = s. Substring (7);

s = s. Substring (11, 6);

//第一个参数是开始位置,第二个参数是截取长度

Console. WriteLine (s );

// Contains() 检测是否包含字串

string s = " 我们的社会真和谐呀!" ;

if (s . Contains (" 社会" ) || s. Contains (" 和谐" ))

{

Console. WriteLine (" 含有敏感词汇,请文明用语!" ); }

// StartWith() bool 以... 开始的字符串

string s = "http:www.cctv.com";

if (s . StartsWith ("http:") || s. StartsWith ("https:")) {

Console. WriteLine (" 是网址" );

}

else

{

Console. WriteLine (" 不是网址" );

}

// IndexOf() 判断某个字符串在另一个字符串中出现的位置 string s = " 你好,我是王某某" ;

int i=s . IndexOf (" 我是" ); //3

int i1 = s. IndexOf (" 你是" );

//如果没有字串的话就返回-1

Console. WriteLine (i );

Console. WriteLine (i1);

,

Console. ReadLine ();

}

}

}

----------------------222222222-----------------------

using System;

using System. Collections . Generic ;

using System. Linq ;

using System. Text ;

namespace 字符串练习

{

class Program

{

static void Main(string [] args)

{

// 1)接受用户输入的一个字符串,将其中的字符串以相反的顺序输出。 // 1234567--->7654321

Console. WriteLine (" 请输入一个字符串:" );

string s1 = Console. ReadLine ();

char [] chars = s1. ToCharArray ();

Console. WriteLine (" 逆置后:" );

for (int i = chars. Length - 1; i >= 0; i--)

{

Console. Write (chars [i ]);

}

// 2)接受用户输入的一句英文,将其中的单词以反序输出。"hello c charp"→"sharp c hello"

Console. WriteLine (" 请输入一句英文:" );

string s = Console. ReadLine ();

char [] a = s. ToCharArray ();

string [] s1 = s. Split (' ');

Console. WriteLine (" 您输入的是:" );

for (int i = 0; i < s1. Length ; i )

{

Console. Write ("{0} ", s1[i ]);

}

Console. WriteLine ();

,

Console. WriteLine (" 逆置后为:" );

for (int i = s1. Length - 1; i >=0; i--)

{

Console. Write ("{0} ", s1[i ]);

}

// 3)从E-mail 中提取出用户名和域名 abc@163.com

string email = "fanyong.net@gmail.com";

string [] s = email. Split ('@');

foreach (string item in s)

{

Console. Write (" 用户名和域名分别是:{0}", s [s . Length -1]); Console. Write (" 用户名和域名分别是:");

Console. WriteLine (item );

}

/* 这个方法相当高明 -- */

Console. WriteLine (" 请输入一个E-mail :" );

string email = Console. ReadLine ();

int atIndex = email. IndexOf ('@');

string username = email. Substring (0, atIndex);

Console. WriteLine (" 用户名是:{0}", username );

string domin = email. Substring (atIndex 1);

Console. WriteLine (" 域名是:{0}", domin);

//4)文本文件中存储了多个文件标题和作者,标题和作者之间用若干空格隔开,每行一个,标题有的

// 长,有的短,输出到控制台的时候标题最多长度为20,如果超出20,则截取长度为17的字串

// 在最后添加... ,加一个竖线|后输出作者的名字。

string [] lines = System. IO . File . ReadAllLines (@"d:1.txt", Encoding . Default );

foreach (string line in lines)

{

string [] strs = line. Split (new char []{'

' },StringSplitOptions . RemoveEmptyEntries );

string title = strs[0];

string author = strs[1];

title = title. Substring (0, Math. Min (17, title. Length )); title = title "..." ;

Console. WriteLine ("{0}|{1}", title , author );

,

}

//5)从ini 格式的文件(每行是" 键=值" 格式) 中读取出配置项的值。 string value = GetConfigValue(@"d:1.ini", " 端口" );

Console. WriteLine (value );

Console. ReadLine ();

}

static string GetConfigValue(string finename, string itemName) {

string [] lines = System. IO . File . ReadAllLines (finename , Encoding . Default );

foreach (string line in lines)

{

string [] strs = line. Split ('=');

string name = strs[0];

string value = strs[1];

if (name . Trim () == itemName)

{

return value. Trim ();

}

}

return "Error!" ;

}

}

}

标签: