c# 字符串常用函数练习
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharp
{
public class TestString
{
public TestString()
{
string[] sA = new string[5] { "H", "e", "l", "l", "o" };
Console.WriteLine(String.Concat(sA));
Console.WriteLine(String.Join("-",sA));
string s = String.Join("", sA);
Console.WriteLine(s);
char[] c = s.ToCharArray();
string sl = s.ToLower();
string su = s.ToUpper();
Console.WriteLine(sl + "--" + su);
string s2 = s.PadLeft(10, '_');
Console.WriteLine(s2);
string s3 = s.PadRight(10, '_');
Console.WriteLine(s3);
string s4 = String.Join("-", sA);
string[] s5 = s4.Split(new char[]{'-'});
foreach (string s6 in s5)
{
Console.WriteLine(s6);
}
string s7 = s2.Trim();
Console.WriteLine(s7);
string s8 = s2.Trim(new char[] { '_' });
Console.WriteLine(s8);
//TrimEnd,TrimStart 同理.
if (s2.StartsWith("H"))
{
Console.WriteLine("S2 was start with 'H'");
}
else
{
Console.WriteLine("S2 wasn't start with 'H'");
}
if (s2.EndsWith("o"))
{
Console.WriteLine("S2 was end with 'o'");
}
else
{
Console.WriteLine("S2 wasn't end with 'o'");
}
Console.WriteLine(s2.Substring(5,s2.Length-5));
Console.WriteLine(s2.Remove(0, 5));
Console.WriteLine(s2.Replace("l","i"));
if (String.IsNullOrEmpty(s2))
{
Console.WriteLine("S2 Is NullOrEmpty");
}
else
{
Console.WriteLine("S2 Isn't NullOrEmpty");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharp
{
public class TestString
{
public TestString()
{
string[] sA = new string[5] { "H", "e", "l", "l", "o" };
Console.WriteLine(String.Concat(sA));
Console.WriteLine(String.Join("-",sA));
string s = String.Join("", sA);
Console.WriteLine(s);
char[] c = s.ToCharArray();
string sl = s.ToLower();
string su = s.ToUpper();
Console.WriteLine(sl + "--" + su);
string s2 = s.PadLeft(10, '_');
Console.WriteLine(s2);
string s3 = s.PadRight(10, '_');
Console.WriteLine(s3);
string s4 = String.Join("-", sA);
string[] s5 = s4.Split(new char[]{'-'});
foreach (string s6 in s5)
{
Console.WriteLine(s6);
}
string s7 = s2.Trim();
Console.WriteLine(s7);
string s8 = s2.Trim(new char[] { '_' });
Console.WriteLine(s8);
//TrimEnd,TrimStart 同理.
if (s2.StartsWith("H"))
{
Console.WriteLine("S2 was start with 'H'");
}
else
{
Console.WriteLine("S2 wasn't start with 'H'");
}
if (s2.EndsWith("o"))
{
Console.WriteLine("S2 was end with 'o'");
}
else
{
Console.WriteLine("S2 wasn't end with 'o'");
}
Console.WriteLine(s2.Substring(5,s2.Length-5));
Console.WriteLine(s2.Remove(0, 5));
Console.WriteLine(s2.Replace("l","i"));
if (String.IsNullOrEmpty(s2))
{
Console.WriteLine("S2 Is NullOrEmpty");
}
else
{
Console.WriteLine("S2 Isn't NullOrEmpty");
}
}
}
}