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 = "[email protected]";
            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:不用记,编译器同意就干就完事啦

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>