获得字符串的某些词

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; } } }

 

转载于:https://www.cnblogs.com/hcrs/p/4510955.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值