格式化字元串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string Str1 = "hello lyshark";
// 取出字元串中指定的字元
char Str2 = Str1[1];
Console.WriteLine("字元: {0} 轉大寫: {1} 轉小寫: {2}", Str2, Char.ToUpper(Str2), Char.ToLower(Str2));
Console.WriteLine("是否為數字: {0} 是否為大寫: {1} 是否為小寫: {2}",
Char.IsNumber(Str2), Char.IsUpper(Str2), Char.IsLower(Str2));
// 将字元串轉化為字元數組
char[] chs = Str1.ToCharArray();
for (int x = 0; x < chs.Length - 1; x++)
Console.Write("{0} ", chs[x]);
Console.WriteLine();
// 将字元數組轉化為字元串
string Str3 = new string(chs);
Console.WriteLine(Str3);
// 格式化輸出字元串
string Str4 = "hello";
string Str5 = "lyshark";
string new_str = String.Format("{0},{1}", Str4, Str5);
Console.WriteLine("格式化後的字元串: {0}", new_str);
Console.ReadKey();
}
}
}
比較字元串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string Str1 = "hello lyshark";
string Str2 = "hello world";
string Str3 = "hello lyshark";
// Compare 比較字元串,相等傳回0不相等傳回-1
Console.WriteLine("Str1 比較 Str2 " + String.Compare(Str1, Str2));
Console.WriteLine("Str1 比較 Str3 " + String.Compare(Str1, Str3));
// Compare To 比較字元串
Console.WriteLine("Str1 比較 Str2 " + Str1.CompareTo(Str2));
Console.WriteLine("Str1 比較 Str3 " + Str1.CompareTo(Str3));
// Equals 比較字元串
Console.WriteLine("Str1 比較 Str2 " + Str1.Equals(Str2));
Console.WriteLine("Str1 比較 Str3 " + String.Equals(Str1,Str3));
Console.ReadKey();
}
}
}
截取/分割字元串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 從第一個位置開始截取3個字元
string Str1 = "hello lyshark";
string Str2 = "";
Str2 = Str1.Substring(1, 3);
Console.WriteLine("截取資料: {0}", Str2);
// 分割字元串變量
string Str3 = "用^一生#下載下傳,百度網盤,資源";
char[] separator = { '^', '#', ',' }; // 定義分割字元
String[] split_string = new String[100];
split_string = Str3.Split(separator);
for (int x = 0; x < split_string.Length;x++ )
{
Console.WriteLine("切割計數: {0} 切割字元串: {1}", x, split_string[x]);
}
// 針對時間的切割方法
string str = "2019-12-12";
char[] chs = { '-' };
string[] date = str.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("{0}年 {1}月 {2}日", date[0], date[1], date[2]);
Console.ReadKey();
}
}
}
插入/删除字元串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 插入字元串的示範
string Str1 = "下載下傳";
Str1 = Str1.Insert(0, "用一生時間");
Console.WriteLine(Str1);
string Str2;
Str2 = Str1.Insert(Str1.Length, "百度網盤裡面的資源");
Console.WriteLine(Str2);
// 填充字元串的示範
string Str3;
Str3 = Str1.PadLeft(Str1.Length + 3, '*'); // 在左側填充
Console.WriteLine("左側填充: " + Str3);
Str3 = Str1.PadRight(Str1.Length + 3, '*'); // 在右側填充
Console.WriteLine("右側填充: " + Str3);
// 去空格的實作
string str = " hahahah ";
str = str.Trim();
str = str.TrimStart();
str = str.TrimEnd();
// 删除字元串的示範
Console.WriteLine("從索引3處向後删除: " + Str3.Remove(3));
Console.WriteLine("删除指定個數的字元: " + Str3.Remove(1, 3));
Console.ReadKey();
}
}
}
拷貝替換字元串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 普通的拷貝字元串
string Str1 = "hello lyshark";
string Str2;
Str2 = string.Copy(Str1);
Console.WriteLine("普通拷貝: " + Str2);
// 替換字元串
string Str3 = "one world,one dream";
string Str4 = Str3.Replace(',','*');
Console.WriteLine("将,替換為** => " + Str4);
string Str5 = Str3.Replace("one", "One");
Console.WriteLine("将one替換為One =>" + Str5);
Console.ReadKey();
}
}
}
尋找開頭結尾字元串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "今天天氣好晴朗,處處好風光";
// 尋找字元串開頭結尾
if (str.StartsWith("今") && str.EndsWith("光"))
Console.WriteLine("ok");
// 從指定字元開始搜尋,并傳回位置
int index = str.IndexOf("天氣", 0);
Console.WriteLine(index);
// 從結束位置開始搜尋
string path = @"c:\a\b\c蒼\d\e蒼\f\g\\fd\fd\fdf\d\vfd\蒼老師.wav";
int path_index = path.LastIndexOf("\\");
Console.WriteLine(path_index);
Console.ReadKey();
}
}
}
串聯字元串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Join 将指定字元串使用|串聯起來
string name_str = string.Join("|", "張三", "李四", "王五", "趙六", "田七");
Console.WriteLine(name_str);
// 将字元串切割後串聯去掉豎線
String[] u_name = { "張三", "李四" ,"王五"};
string ptr = string.Join("|", u_name);
Console.WriteLine("合并後: " + ptr);
string[] strNew = ptr.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("去掉| = " + strNew[1]);
for (int x = 0; x < strNew.Length;x++ )
Console.WriteLine("去掉豎線 [{0}] = {1}",x,strNew[x]);
Console.ReadKey();
}
}
}
字元串倒序輸出:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "hello lyshark";
// 實作反轉字元串 hello -> olleh
char[] chs = str.ToCharArray();
for (int x = 0; x < chs.Length / 2;x++ )
{
char tmp = chs[x];
chs[x] = chs[chs.Length - 1 - x];
chs[chs.Length - 1 - x] = tmp;
}
str = new string(chs);
Console.WriteLine("反轉後的結果: {0}", str);
// 實作反轉單詞 hello lyshark -> lyshark hello
string str1 = "hello lyshark";
string[] strNew = str1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int x = 0; x < strNew.Length / 2; x++)
{
string tmp = strNew[x];
strNew[x] = strNew[strNew.Length - 1 - x];
strNew[strNew.Length - 1 - x] = tmp;
}
str1 = string.Join(" ", strNew);
Console.WriteLine("反轉後的結果: {0}", str1);
Console.ReadKey();
}
}
}
IndexOf搜尋字元串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 通過indexOf 切割特定字元
string email = "[email protected]";
int index = email.IndexOf("@");
string userName = email.Substring(0, index);
string userHost = email.Substring(index + 1);
Console.WriteLine("名字: {0} 主機: {1}",userName,userHost);
// 尋找指定字元出現位置
string str = "abcd wwabcd asdcdsac waascd ascsaaa";
int index1 = str.IndexOf('a');
int count = 1;
while(index1 != -1)
{
count++;
index1 = str.IndexOf('a', index1 + 1);
if (index1 == -1)
break;
Console.WriteLine("第{0}次出現a的位置是{1}", count, index1);
}
Console.ReadKey();
}
}
}
版權聲明:本部落格文章與代碼均為學習時整理的筆記,文章 [均為原創] 作品,轉載請 [添加出處] ,您添加出處是我創作的動力!