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 字符串基礎(chǔ)
{
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 = " 我是杰克遜我是麥當(dāng)娜我是鳳姐我是小甜甜" ; string [] strs = s1. Split (new string [] { " 我是" }, StringSplitOptions . RemoveEmptyEntries );
//杰克遜 麥當(dāng)娜 鳳姐 小甜甜
foreach (var item in strs)
{
Console. WriteLine (item );
}
//練習(xí)
//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() 應(yīng)用
string s = " 李時(shí)珍同志是一名好同志,向李時(shí)珍同志學(xué)習(xí)!" ; s = s. Replace (" 李時(shí)珍" , " 李素麗" );
Console. WriteLine (s );
// SbuString() 函數(shù) 從指定序號(hào)開(kāi)始一直到字符串的末尾結(jié)束 string s = "http://www.google.com";
s = s. Substring (7);
s = s. Substring (11, 6);
//第一個(gè)參數(shù)是開(kāi)始位置,第二個(gè)參數(shù)是截取長(zhǎng)度
Console. WriteLine (s );
// Contains() 檢測(cè)是否包含字串
string s = " 我們的社會(huì)真和諧呀!" ;
if (s . Contains (" 社會(huì)" ) || s. Contains (" 和諧" ))
{
Console. WriteLine (" 含有敏感詞匯,請(qǐng)文明用語(yǔ)!" ); }
// StartWith() bool 以... 開(kāi)始的字符串
string s = "http:www.cctv.com";
if (s . StartsWith ("http:") || s. StartsWith ("https:")) {
Console. WriteLine (" 是網(wǎng)址" );
}
else
{
Console. WriteLine (" 不是網(wǎng)址" );
}
// IndexOf() 判斷某個(gè)字符串在另一個(gè)字符串中出現(xiàn)的位置 string s = " 你好,我是王某某" ;
int i=s . IndexOf (" 我是" ); //3
int i1 = s. IndexOf (" 你是" );
//如果沒(méi)有字串的話就返回-1
Console. WriteLine (i );
Console. WriteLine (i1);
,Console. ReadLine ();
}
}
}
----------------------222222222-----------------------
using System;
using System. Collections . Generic ;
using System. Linq ;
using System. Text ;
namespace 字符串練習(xí)
{
class Program
{
static void Main(string [] args)
{
// 1)接受用戶輸入的一個(gè)字符串,將其中的字符串以相反的順序輸出。 // 1234567--->7654321
Console. WriteLine (" 請(qǐng)輸入一個(gè)字符串:" );
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 (" 請(qǐng)輸入一句英文:" );
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 );
}
/* 這個(gè)方法相當(dāng)高明 -- */
Console. WriteLine (" 請(qǐng)輸入一個(gè)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)文本文件中存儲(chǔ)了多個(gè)文件標(biāo)題和作者,標(biāo)題和作者之間用若干空格隔開(kāi),每行一個(gè),標(biāo)題有的
// 長(zhǎng),有的短,輸出到控制臺(tái)的時(shí)候標(biāo)題最多長(zhǎng)度為20,如果超出20,則截取長(zhǎng)度為17的字串
// 在最后添加... ,加一個(gè)豎線|后輸出作者的名字。
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 格式的文件(每行是" 鍵=值" 格式) 中讀取出配置項(xiàng)的值。 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!" ;
}
}
}