何时使用字符
遇到字符
'\'
时出现的错误
转义字符使用技巧
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
String str = "时间就是金钱";
Console.WriteLine(str);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
char[] array = { '时','间','就','是','金','钱'};
string str = new string(array);
string str2 = new string(array,4,2);
Console.WriteLine(str);
Console.WriteLine(str2);
Console.ReadLine();
}
}
}
判断用户名是否存在
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
string str ="mr1";
string str2 = "mr12";
Console.WriteLine(string.Compare(str,str2));
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
string str ="mr1";
string str2 = "mr12";
Console.WriteLine(str.CompareTo(str2));
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
string str ="mr1";
string str2 = "mr12";
Console.WriteLine(str.Equals(str2));
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("货币形式:{0:C}",365);
Console.WriteLine("科学计数法:{0:E}",12);
Console.WriteLine("货币形式:{0:N}",36534);
Console.WriteLine("Π取两位小数:{0:F2}",Math.PI);
Console.WriteLine("16进制显示:{0:X4}",36);
Console.WriteLine("百分比显示:{0:P}",0.99);
Console.ReadLine();
}
}
}
从身份证中获取出生日期
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
class Program
{
static void Main(string[] args)
{
string fileName = "glorysoft.com";
string file = fileName.Substring(0, fileName.IndexOf('.'));
string fileT = fileName.Substring(fileName.IndexOf('.'));
Console.WriteLine(file);
Console.WriteLine(fileT);
Console.ReadLine();
}
}
}
索引或者长度超出字符串范围得错误
限定分割次数
限定分割次数得执行效果
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
class Program
{
static void Main(string[] args)
{
string fileName = "glorysoft.com.com.com";
string[] array = fileName.Split(new char[] { '.' },2);
for (int i = 0; i < array.Length; i++) {
Console.WriteLine(array[i]);
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
string old = "you are a pig";
Console.WriteLine(old);
string newOld = old.Insert(8, "to");
Console.WriteLine(newOld);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
string old = "you are a pig";
Console.WriteLine(old);
string newOld = old.Remove(4);
string newOld2 = old.Remove(4,7);
Console.WriteLine(newOld);
Console.WriteLine(newOld2);
Console.ReadLine();
}
}
}
复制字符串的一部分
替换字符串中的字符
替换字符串中的子字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
string str = "馒头一文一个";
Console.WriteLine(str);
string str2 = str.Replace("一", "壹");
string str3 = str.Replace("馒头","馍馍");
Console.WriteLine(str2);
Console.WriteLine(str3);
Console.ReadLine();
}
}
}
替换字符串需要注意的事项
定义:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder("(),(),(),4, 6, 7、8)");
Console.WriteLine(sb);
sb.Remove(0, 9);
sb.Insert(0,"(门前大桥下),(游过一群鸭),(快来快来数一数),");
Console.WriteLine(sb);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
long start = DateTime.Now.Millisecond;
string a = "";
for (int i = 0; i <= 10000; i++) {
a += i;
}
long end = DateTime.Now.Millisecond;
Console.WriteLine(end - start);
StringBuilder sb = new StringBuilder();
long start1 = DateTime.Now.Millisecond;
for (int j=0; j < 10000;j++) {
sb.Append(j);
}
long end2 = DateTime.Now.Millisecond;
Console.WriteLine(end2-start1);
Console.ReadLine();
}
}
}