C# visual studio 2022学习1

1.

  • Trim()去掉字符串头尾的空格
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace csstudy
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string s= "www    lll";//去掉字符串多余空格
            //Trim去掉字符串两边空格
            string s1 = s.Substring(0, s.IndexOf(' '));
            Console.WriteLine(s1+s.Substring(s.IndexOf(' ')).Trim());
            Console.ReadLine();  
        }
    }
}

2.

tab是快捷键,表示接受选项卡

Console.WriteLine(s1);

Console.ReadLine();可以让黑屏幕停留一下

Console.ReadKey()当然也可以输出字符串

 Console.WriteLine(1.0);
 Console.ReadKey();//输出1,readkey是int类型
string s = "012345666789";

找索引:

int i = s.IndexOf("6");
int j = s.LastIndexOf("6");
Console.WriteLine(i + "-" + j);//输出6-8

任意找索引:char[]数组里面必须字符啦,indexofany是找任意字符的第一个索引位置

 char[] chars = { '9','6','0' };// char[]数组赋值,提词器是这个格式
 int k = s.IndexOfAny(chars);//输出0

substring(a,b),[a,b)

substring(a),[a,rest

string s = "012345666789";
string s1 = s.Substring(0, 2);
Console.WriteLine(s1);//输出01
Console.ReadLine();

3.

修改文件名

string path = "f1.txt";
int location = path.LastIndexOf('.');
string s = path.Substring(0, location) + ".doc";//location之前的东西
Console.WriteLine(s);
Console.ReadLine();

4.

Regex i=new Regex(正则表达式的格式)

i.IsMatch(用来判断的案例)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace csstudy
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //写正则表达式匹配
            string emailme = "yatingyang2004@qq.com";
            string pattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
            Regex regex = new Regex(pattern);
            if (regex.IsMatch(emailme))
            {
                Console.WriteLine("yes!");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("no!");
                Console.ReadLine();
            }
        }
    }
}

5.小tips:不用记,编译器同意就干就完事啦

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值