using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 字符串练习 { class Program { static void Main(string[] args) { t2(); } #region t1 // public static void t1() { Console.WriteLine("输入一个字符串,马上就给你反转过来"); string input = Console.ReadLine(); string[] wds = input.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < wds.Length; i++) { wds[i] = ResStr(wds[i]); } string rs = string.Join(" ", wds); Console.WriteLine(rs); Console.ReadKey(); } #endregion public static void t2() { string str = "今天是201年34月34日"; int yearIndex = str.IndexOf("年");//字符串从0开始计数,yearIndex为6 int getStartIndex = GetStart(str, yearIndex); int mouthIndex = str.IndexOf("月"); int dayIndex = str.IndexOf("日"); string yStr = str.Substring(getStartIndex+1, yearIndex-getStartIndex-1); string mStr = str.Substring(yearIndex+1, mouthIndex - yearIndex - 1); string dStr = str.Substring(mouthIndex+1, dayIndex - mouthIndex - 1); Console.WriteLine(yStr); Console.WriteLine(mStr); Console.WriteLine(dStr); Console.ReadKey(); }
public static void t3()//读出csv文件内容
{
string[] str = File.ReadAllLines("t.csv",Encoding.Default);
for (int i = 0; i < str.Length; i++)
{
string[] juti = str[i].Split(',');
Console.WriteLine("姓名{0};电话{1}",juti);//标准流,所以不需要使用juti[0],juti[1]这样的形式
}
Console.ReadKey();
} private static string ResStr(string input) { char[] wd = input.ToCharArray(); for (int i = 0; i < wd.Length/2; i++) { char temp = wd[i]; wd[i] = wd[wd.Length - i-1]; wd[wd.Length - i - 1] = temp; } return new string(wd); } //获得起始位置下标 private static int GetStart(string str,int yearIndex) { for (int i = yearIndex; i >=0; i--) { if (!char.IsDigit(str[i]))//判断是否为十进制数 { return i; } } return -1; } } }