汇总系列:https://www.cnblogs.com/dunitian/p/4822808.html#ai

Jupyter排版:https://www.cnblogs.com/dunitian/p/9119986.html

在线浏览:http://nbviewer.jupyter.org/github/lotapp/BaseCode/tree/master/python/notebook/1.POP/2.str

事先声明一下,避免让新手进入误区:不是说Python比NetCore要好,而Python设计的目的就是==》让程序员解放出来,不要过于关注代码本身,那么性能、规范等等各方面隐患就存在了,后面编写一个稍微大点的项目就看出来了。而且不要太受语言约束,之前我也说过,用各自语言的优势来为项目服务~ 这才是开发王道。比如Python用来数据分析,Go用来并发处理等等,不多说了,记住一句话即可:“Net是性价比最高的”

步入正题:欢迎提出更简单或者效率更高的方法

基础系列:(这边重点说说Python,上次讲过的东西我就一笔带过了)

1.输出+类型转换
Python写法:

NetCore:

2.字符串拼接+拼接输出方式

python:

NetCore

3.字符串遍历、下标、切片

重点说下python的下标,有点意思,最后一个元素,我们一般都是len(str)-1,他可以直接用-1,倒2自然就是-2

#最后一个元素:user_str[-1]
user_str[-1]
user_str[len(user_str)-1] #其他编程语言写法
#倒数第二个元素:user_str[-2]

这次为了更加形象对比,一句一句翻译成NetCore(有没有发现规律,user_str[user_str.Length-1]==》-1是最后一个,user_str[user_str.Length-2]==》-2是最后一个。python在这方面简化了

3.2 python切片语法:[start_index:end_index:step] (end_index取不到)

# 切片:[start_index:end_index:step] (end_index取不到)
# eg:str[1:4] 取str[1]、str[2]、str[3]
# eg:str[2:] 取下标为2开始到最后的元素
# eg:str[2:-1] 取下标为2~到倒数第二个元素(end_index取不到)
# eg:str[1:6:2] 隔着取~str[1]、str[3]、str[5](案例会详细说)
# eg:str[::-1] 逆向输出(案例会详细说,)

来个案例:我注释部分说的很详细了,附录会贴democode的

NetCore,其实你用Python跟其他语言对比反差更大,net真的很强大了。补充(对比看就清楚Python的step为什么是2了,i+=2==》2)

方法系列:

# 查找:find,rfind,index,rindex
Python查找推荐你用find和rfind
netcore:index0f就相当于python里面的find

# 计数:count
python:str.count()
netcore:这个真用基础来解决的话,两种方法:
第一种自己变形一下:(原字符串长度 - 替换后的长度) / 字符串长度
 

            int count = ;
int index = input.IndexOf("abc"); while (index != -)
{
count++;
index = input.IndexOf("abc", index + );//index指向abc的后一位
}
Python补充说明:像这些方法练习用ipython3就好了(sudo apt-get install ipython3),code的话需要一个个的print,比较麻烦(我这边因为需要写文章,所以只能一个个code)
index查找不到会有异常

# 替换:replace
Python:xxx.replace(str1, str2, 替换次数)
replace可以指定替换几次

NetCore:替换指定次数的功能有点业余,就不说了,你可以自行思考哦~


#连接:join:eg:print("-".join(test_list))

netcore:string.Join(分隔符,数组)

#分割:split(按指定字符分割),splitlines(按行分割),partition(以str分割成三部分,str前,str和str后),rpartition

说下split的切片用法 :print(test_input.split(" ",3)) #在第三个空格处切片,后面的不切了

继续说说splitlines(按行分割),和split("\n")的区别我图中给了案例
扩展:split(),默认按空字符切割(空格、\t、\n等等,不用担心返回'')
最后说一下partitionrpartition 返回是元祖类型(后面会说的),方式和find一样,找到第一个匹配的就罢工了【注意一下没找到的情况】

 
netcore: split里面很多重载方法,可以自己去查看下,eg:Split("\n",StringSplitOptions.RemoveEmptyEntries)
再说一下这个:test_str.Split('a');//返回数组如果要和Python一样返回列表==》test_str.Split('a').ToList(); 【需要引用linq的命名空间哦】

# 头尾判断:startswith(以。。。开头),endswith(以。。。结尾)

netcore:


# 大小写系:lower(字符串转换为小写),upper(字符串转换为大写),title(单词首字母大写),capitalize(第一个字符大写,其他变小写)

netcore:


# 格式系列:lstrip(去除左边空格),rstrip(去除右边空格),strip(去除两边空格)美化输出系列:ljust,rjust,center
 
 
netcore:Tirm很强大,除了去空格还可以去除你想去除的任意字符
ljust,rjust,center这些就不说了,python经常在linux终端中输出,所以这几个用的比较多。net里面string.Format各种格式化输出,可以参考
 

# 验证系列:isalpha(是否是纯字母),isalnum(是否是数字|字母),isdigit(是否是纯数字),isspace(是否是纯空格)
一张图搞定,其他的自己去试一试吧,注意哦~ test_str5=" \t \n " #isspace() ==>true
 
netcore:string.IsNullOrEmpty 和 string.IsNullOrWhiteSpace 是系统自带的,其他的你需要自己封装一个扩展类(eg:简单封装)【附录有】

附录:

Python3:

# #输出+类型转换
# user_num1=input("输入第一个数:")
# user_num2=input("输入第二个数:") # print("两数之和:%d"%(int(user_num1)+int(user_num2))) # # ------------------------------------------------------------ # #字符串拼接
# user_name=input("输入昵称:")
# user_pass=input("输入密码:")
# user_url="192.168.1.121" # #拼接输出方式一:
# print("ftp://"+user_name+":"+user_pass+"@"+user_url) # #拼接输出方式二:
# print("ftp://%s:%s@%s"%(user_name,user_pass,user_url)) # # ------------------------------------------------------------- # # 字符串遍历、下标、切片
# user_str="七大姑曰:工作了吗?八大姨问:买房了吗?异性说:结婚了吗?" # #遍历
# for item in user_str:
# print(item,end=" ") # #长度:len(user_str)
# print(len(user_str)) # #第一个元素:user_str[0]
# print(user_str[0]) # #最后一个元素:user_str[-1]
# print(user_str[-1])
# print(user_str[len(user_str)-1])#其他编程语言写法 # #倒数第二个元素:user_str[-2]
# print(user_str[-2]) # # ------------------------------------------------------------- # 切片:[start_index:end_index:step] (end_index取不到)
# eg:str[1:4] 取str[1]、str[2]、str[3]
# eg:str[2:] 取下标为2开始到最后的元素
# eg:str[2:-1] 取下标为2~到倒数第二个元素(end_index取不到)
# eg:str[1:6:2] 隔着取~str[1]、str[3]、str[5](案例会详细说)
# eg:str[::-1] 逆向输出(案例会详细说,) it_str="我爱编程,编程爱它,它是程序,程序是谁?" #eg:取“编程爱它” it_str[5:9]
print(it_str[5:9])
print(it_str[5:-11]) #end_index用-xx也一样
print(it_str[-15:-11])#start_index用-xx也可以 #eg:取“编程爱它,它是程序,程序是谁?” it_str[5:]
print(it_str[5:])#不写默认取到最后一个 #eg:一个隔一个跳着取("我编,程它它程,序谁") it_str[0::2]
print(it_str[0::2])#step=△index(eg:0,1,2,3。这里的step=> 2-0 => 间隔1) #eg:倒序输出 it_str[::-1]
# end_index不写默认是取到最后一个,是正取(从左往右)还是逆取(从右往左),就看step是正是负
print(it_str[::-1])
print(it_str[-1::-1])#等价于上一个 # # -------------------------------------------------------------
test_str="ABCDabcdefacddbdf"

# -------------------------------------------------------------

# # 查找:find,rfind,index,rindex
# # xxx.find(str, start, end)
# print(test_str.find("cd"))#从左往右
# print(test_str.rfind("cd"))#从右往左
# print(test_str.find("dnt"))#find和rfind找不到就返回-1 # # index和rindex用法和find一样,只是找不到会报错(以后用find系即可)
# # print(test_str.index("dnt")) # # ------------------------------------------------------------- # # 计数:count
# # xxx.count(str, start, end)
# print(test_str.count("a")) # # ------------------------------------------------------------- # # 替换:replace
# # xxx.replace(str1, str2, count_num)
# print(test_str)
# print(test_str.replace("b","B"))#并没有改变原字符串,只是生成了一个新的字符串
# print(test_str) # # replace可以指定替换几次
# print(test_str.replace("b","B",1))#ABCDaBcdefacddbdf # # ------------------------------------------------------------- # 分割:split(按指定字符分割),splitlines(按行分割),,partition(以str分割成三部分,str前,str和str后),rpartition
# test_list=test_str.split("a")#a有两个,按照a分割,那么会分成三段,返回类型是列表(List),并且返回结果中没有a
# print(test_list) # test_input="hi my name is dnt"
# print(test_input.split(" ")) #返回列表格式(后面会说)['hi', 'my', 'name', 'is', 'dnt']
# print(test_input.split(" ",3))#在第三个空格处切片,后面的不管了 # # 按行分割,返回类型为List
# test_line_str="abc\nbca\ncab\n"
# print(test_line_str.splitlines())#['abc', 'bca', 'cab']
# print(test_line_str.split("\n"))#看出区别了吧:['abc', 'bca', 'cab', ''] # # 没看出来就再来个案例
# test_line_str2="abc\nbca\ncab\nLLL"
# print(test_line_str2.splitlines())#['abc', 'bca', 'cab', 'LLL']
# print(test_line_str2.split("\n"))#再提示一下,最后不是\n就和上面一样效果 # 扩展:
# print("hi my name is dnt\t\n m\n\t\n".split())#split(),默认按空字符切割(空格、\t、\n等等,不用担心返回'') # #partition,返回是元祖类型(后面会说的),方式和find一样,找到第一个匹配的就罢工了
# print(test_str.partition("cd"))#('ABCDab', 'cd', 'efacddbdf')
# print(test_str.rpartition("cd"))#('ABCDabcdefa', 'cd', 'dbdf')
# print(test_str.partition("感觉自己萌萌哒"))#没找到:('ABCDabcdefacddbdf', '', '') # # ------------------------------------------------------------- # # 连接:join
# # separat.join(xxx)
# # 错误用法:xxx.join("-")
# print("-".join(test_list)) # # ------------------------------------------------------------- # # 头尾判断:startswith(以。。。开头),endswith(以。。。结尾)
# # test_str.startswith(以。。。开头)
# start_end_str="http://www.baidu.net"
# print(start_end_str.startswith("https://") or start_end_str.startswith("http://"))
# print(start_end_str.endswith(".com"))
# # ------------------------------------------------------------- # # 大小写系:lower(字符串转换为小写),upper(字符串转换为大写),title(单词首字母大写),capitalize(第一个字符大写,其他变小写)
# print(test_str)
# print(test_str.upper())#ABCDABCDEFACDDBDF
# print(test_str.lower())#abcdabcdefacddbdf
# print(test_str.capitalize())#第一个字符大写,其他变小写 # # ------------------------------------------------------------- # # 格式系列:lstrip(去除左边空格),rstrip(去除右边空格),strip(去除两边空格),ljust,rjust,center
# strip_str=" I Have a Dream "
# print(strip_str.strip()+"|")#我加 | 是为了看清后面空格,没有别的用处
# print(strip_str.lstrip()+"|")
# print(strip_str.rstrip()+"|") # #这个就是格式化输出,就不讲了
# print(test_str.ljust(50))
# print(test_str.rjust(50))
# print(test_str.center(50))
# # ------------------------------------------------------------- # 验证系列:isalpha(是否是纯字母),isalnum(是否是数字|字母),isdigit(是否是纯数字),isspace(是否是纯空格) # test_str2="Abcd123"
# test_str3="123456"
# test_str4=" \t"
# test_str5=" \t \n " #isspace() ==>true
# 一张图搞定,其他的自己去试一试吧
# test_str.isalpha()
# test_str.isalnum()
# test_str.isdigit()
# test_str.isspace()

NetCore:

using System;
using System.Linq; namespace aibaseConsole
{
public static class Program
{
private static void Main()
{
#region BaseCode //var test="123";//定义一个变量
// Console.WriteLine(test);//输出这个变量
//
// Console.WriteLine("请输入用户名:");
// var name = Console.ReadLine();
//
// Console.WriteLine("请输入性别:");
// var gender = Console.ReadLine();
//
// Console.WriteLine($"Name:{name},Gender:{gender}"); //推荐用法
// Console.WriteLine("Name:{0},Gender:{1}", name, gender); //Old 输出
//
//// 类型转换
// Console.WriteLine("输入第一个数字:");
// var num1 = Console.ReadLine();
// Console.WriteLine("输入第二个数字:");
// var num2 = Console.ReadLine();
// Console.WriteLine($"num1+num2={Convert.ToInt32(num1)+Convert.ToInt32(num2)}");
//
//// Convert.ToInt64(),Convert.ToDouble(),Convert.ToString()
// Console.Write("dnt.dkill.net/now");
// Console.WriteLine("带你走进中医经络");
//
// var temp = "xxx";
// var tEmp = "===";
// Console.WriteLine(temp + tEmp);
// var num = 9;
// Console.WriteLine("num=9,下面结果是对2的除,取余,取商操作:");
// Console.WriteLine(num/2.0);
// Console.WriteLine(num%2.0);
// Console.WriteLine(num/2);
// //指数
// Console.WriteLine(Math.Pow(2,3)); // int age = 24;
//
// if (age >= 23)
// Console.WriteLine("七大姑曰:工作了吗?八大姨问:买房了吗?异性说:结婚了吗?");
// else if (age >= 18)
// {
// Console.WriteLine(age);
// Console.WriteLine("成年了哇");
// }
// else
// Console.WriteLine("好好学习天天向上"); // int i = 1;
// int sum = 0;
// while (i <= 100)
// {
// sum += i;
// i++;
// }
// Console.WriteLine(sum); // var name = "https://pan.baidu.com/s/1weaF2DGsgDzAcniRzNqfyQ#mmd";
// foreach (var i in name)
// {
// if(i=='#')
// break;
// Console.Write(i);
// }
// Console.WriteLine("\n end ..."); #endregion #region String
// //# # 字符串遍历、下标、切片
// //# user_str="七大姑曰:工作了吗?八大姨问:买房了吗?异性说:结婚了吗?"
// var user_str = "七大姑曰:工作了吗?八大姨问:买房了吗?异性说:结婚了吗?";
//
// //# #遍历
// //# for item in user_str:
// //# print(item,end=" ")
// foreach (var item in user_str)
// {
// Console.Write(item);
// }
//
// //# #长度:len(user_str)
// //# print(len(user_str))
// Console.WriteLine(user_str.Length);
//
// //# #第一个元素:user_str[0]
// //# print(user_str[0])
// Console.WriteLine(user_str[0]);
//
// //# #最后一个元素:user_str[-1]
// //# print(user_str[-1])
// //# print(user_str[len(user_str)-1])#其他编程语言写法
// Console.WriteLine(user_str[user_str.Length-1]);
// //
// //# #倒数第二个元素:user_str[-2]
// //# print(user_str[-2])
// Console.WriteLine(user_str[user_str.Length-2]); // //# # -------------------------------------------------------------
// //
// //# 切片:[start_index:end_index:step] (end_index取不到)
// //# eg:str[1:4] 取str[1]、str[2]、str[3]
// //# eg:str[2:] 取下标为2开始到最后的元素
// //# eg:str[2:-1] 取下标为2~到倒数第二个元素(end_index取不到)
// //# eg:str[1:6:2] 隔着取~str[1]、str[3]、str[5](案例会详细说)
// //# eg:str[::-1] 逆向输出(案例会详细说,)
// //
// var it_str = "我爱编程,编程爱它,它是程序,程序是谁?";
// //
// //#eg:取“编程爱它” it_str[5:9]
// // print(it_str[5:9])
// // print(it_str[5:-11]) #end_index用-xx也一样
// // print(it_str[-15:-11])#start_index用-xx也可以
//
// //Substring(int startIndex, int length)
// Console.WriteLine(it_str.Substring(5, 4));//第二个参数是长度
//
// //
// //#eg:取“编程爱它,它是程序,程序是谁?” it_str[5:]
// // print(it_str[5:])#不写默认取到最后一个
// Console.WriteLine(it_str.Substring(5));//不写默认取到最后一个
//
// //#eg:一个隔一个跳着取("我编,程它它程,序谁") it_str[0::2]
// // print(it_str[0::2])#step=△index(eg:0,1,2,3。这里的step=> 2-0 => 间隔1)
//
// //这个我第一反应是用linq ^_^
// for (int i = 0; i < it_str.Length; i+=2)//对比看就清除Python的step为什么是2了,i+=2==》2
// {
// Console.Write(it_str[i]);
// }
//
// Console.WriteLine("\n倒序:");
// //#eg:倒序输出 it_str[::-1]
// //# end_index不写默认是取到最后一个,是正取(从左往右)还是逆取(从右往左),就看step是正是负
// // print(it_str[::-1])
// // print(it_str[-1::-1])#等价于上一个
// for (int i = it_str.Length-1; i>=0; i--)
// {
// Console.Write(it_str[i]);
// }
// //其实可以用Linq:Console.WriteLine(new string(it_str.ToCharArray().Reverse().ToArray()));
#endregion #region StringMethod
// var test_str = "ABCDabcdefacddbdf"; // //# # 查找:find,rfind,index,rindex
// //# # xxx.find(str, start, end)
// //# print(test_str.find("cd"))#从左往右
// Console.WriteLine(test_str.IndexOf('a'));//4
// Console.WriteLine(test_str.IndexOf("cd"));//6 // //# print(test_str.rfind("cd"))#从右往左
// Console.WriteLine(test_str.LastIndexOf("cd"));//11 // //# print(test_str.find("dnt"))#find和rfind找不到就返回-1
// Console.WriteLine(test_str.IndexOf("dnt"));//-1
// //# # index和rindex用法和find一样,只是找不到会报错(以后用find系即可)
// //# print(test_str.index("dnt")) // //# # -------------------------------------------------------------
// //# # 计数:count
// //# # xxx.count(str, start, end)
// // print(test_str.count("d"))#4
// // print(test_str.count("cd"))#2
// // 第一反应,字典、正则、linq,后来想怎么用基础知识解决,于是有了这个~(原字符串长度-替换后的长度)/字符串长度 // System.Console.WriteLine(test_str.Length-test_str.Replace("d","").Length);//统计单个字符就简单了
// System.Console.WriteLine((test_str.Length-test_str.Replace("cd","").Length)/"cd".Length);
// System.Console.WriteLine(test_str);//不用担心原字符串改变(python和C#都是有字符串不可变性的) // //# # -------------------------------------------------------------
// //
// //# # 替换:replace
// //# # xxx.replace(str1, str2, count_num)
// //# print(test_str)
// //# print(test_str.replace("b","B"))#并没有改变原字符串,只是生成了一个新的字符串
// //# print(test_str)
// System.Console.WriteLine(test_str.Replace("b","B"));
// //# # replace可以指定替换几次
// //# print(test_str.replace("b","B",1))#ABCDaBcdefacddbdf
// // 替换指定次数的功能有点业余,就不说了 // //# # ------------------------------------------------------------- // //# 分割:split(按指定字符分割),splitlines(按行分割),partition(以str分割成三部分,str前,str和str后),rpartition
// //# test_list=test_str.split("a")#a有两个,按照a分割,那么会分成三段,返回类型是列表(List),并且返回结果中没有a
// //# print(test_list)
// var test_array = test_str.Split('a');//返回数组(如果要返回列表==》test_str.Split('a').ToList();)
// var test_input = "hi my name is dnt";
// //# print(test_input.split(" ")) #返回列表格式(后面会说)['hi', 'my', 'name', 'is', 'dnt']
// test_input.Split(" ");
// //# print(test_input.split(" ",3))#在第三个空格处切片,后面的不管了
// //# # 按行分割,返回类型为List
// var test_line_str = "abc\nbca\ncab\n";
// //# print(test_line_str.splitlines())#['abc', 'bca', 'cab']
// test_line_str.Split("\n", StringSplitOptions.RemoveEmptyEntries);
// //# print(test_line_str.split("\n"))#看出区别了吧:['abc', 'bca', 'cab', '']
// //
// //# # # 没看出来就再来个案例
// //# test_line_str2="abc\nbca\ncab\nLLL"
// //# print(test_line_str2.splitlines())#['abc', 'bca', 'cab', 'LLL']
// //# print(test_line_str2.split("\n"))#再提示一下,最后不是\n就和上面一样效果
// //
// //# # 扩展:
// //# print("hi my name is dnt\t\n m\n\t\n".split())#split(),默认按空字符切割(空格、\t、\n等等,不用担心返回'')
// //
// //# #partition,返回是元祖类型(后面会说的),方式和find一样,找到第一个匹配的就罢工了
// //# print(test_str.partition("cd"))#('ABCDab', 'cd', 'efacddbdf')
// //# print(test_str.rpartition("cd"))#('ABCDabcdefa', 'cd', 'dbdf')
// //# print(test_str.partition("感觉自己萌萌哒"))#没找到:('ABCDabcdefacddbdf', '', '')
// //
// //# # -------------------------------------------------------------
// //# 连接:join
// //# separat.join(xxx)
// //# 错误用法:xxx.join("-")
// //# print("-".join(test_list))
// System.Console.WriteLine(string.Join("-",test_array));//test_array是数组 ABCD-bcdef-cddbdf
//# # -------------------------------------------------------------
//
// //# # 头尾判断:startswith(以。。。开头),endswith(以。。。结尾)
// //# # test_str.startswith(以。。。开头)
// var start_end_str="http://www.baidu.net";
// //# print(start_end_str.startswith("https://") or start_end_str.startswith("http://"))
// System.Console.WriteLine(start_end_str.StartsWith("https://")||start_end_str.StartsWith("http://"));
// //# print(start_end_str.endswith(".com"))
// System.Console.WriteLine(start_end_str.EndsWith(".com"));
// //# # -------------------------------------------------------------
// //
// //# # 大小写系:lower(字符串转换为小写),upper(字符串转换为大写),title(单词首字母大写),capitalize(第一个字符大写,其他变小写)
// //# print(test_str)
// //# print(test_str.upper())#ABCDABCDEFACDDBDF
// System.Console.WriteLine(test_str.ToUpper());
// //# print(test_str.lower())#abcdabcdefacddbdf
// System.Console.WriteLine(test_str.ToLower());
// //# print(test_str.capitalize())#第一个字符大写,其他变小写
// //
// //# # -------------------------------------------------------------
// //
// //# # 格式系列:lstrip(去除左边空格),rstrip(去除右边空格),strip(去除两边空格),ljust,rjust,center
// var strip_str=" I Have a Dream ";
// //# print(strip_str.strip()+"|")#我加 | 是为了看清后面空格,没有别的用处
// System.Console.WriteLine(strip_str.Trim()+"|");
// //# print(strip_str.lstrip()+"|")
// System.Console.WriteLine(strip_str.TrimStart()+"|");
// //# print(strip_str.rstrip()+"|")
// System.Console.WriteLine(strip_str.TrimEnd()+"|"); //# #这个就是格式化输出,就不讲了
//# print(test_str.ljust(50))
//# print(test_str.rjust(50))
//# print(test_str.center(50))
//# # -------------------------------------------------------------
//
// //# 验证系列:isalpha(是否是纯字母),isalnum(是否是数字|字母),isdigit(是否是纯数字),isspace(是否是纯空格)
// //
// //# test_str2="Abcd123"
// //# test_str3="123456"
// var test_str4=" \t";
// var test_str5=" \t \n "; //#isspace() ==>true
// // string.IsNullOrEmpty 和 string.IsNullOrWhiteSpace 是系统自带的,其他的你需要自己封装一个扩展类
// System.Console.WriteLine(string.IsNullOrEmpty(test_str4)); //false
// System.Console.WriteLine(string.IsNullOrWhiteSpace(test_str4));//true
// System.Console.WriteLine(string.IsNullOrEmpty(test_str5));//false
// System.Console.WriteLine(string.IsNullOrWhiteSpace(test_str5));//true
// //# 一张图搞定,其他的自己去试一试吧
// //# test_str.isalpha()
// //# test_str.isalnum()
// //# test_str.isdigit()
// //# test_str.isspace()
#endregion
// Console.Read();
}
}
}

简单封装:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions; public static partial class ValidationHelper
{
#region 常用验证 #region 集合系列
/// <summary>
/// 判断集合是否有数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static bool ExistsData<T>(this IEnumerable<T> list)
{
bool b = false;
if (list != null && list.Count() > )
{
b = true;
}
return b;
}
#endregion #region Null判断系列
/// <summary>
/// 判断是否为空或Null
/// </summary>
/// <param name="objStr"></param>
/// <returns></returns>
public static bool IsNullOrWhiteSpace(this string objStr)
{
if (string.IsNullOrWhiteSpace(objStr))
{
return true;
}
else
{
return false;
}
} /// <summary>
/// 判断类型是否为可空类型
/// </summary>
/// <param name="theType"></param>
/// <returns></returns>
public static bool IsNullableType(Type theType)
{
return (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
}
#endregion #region 数字字符串检查
/// <summary>
/// 是否数字字符串(包括小数)
/// </summary>
/// <param name="objStr">输入字符串</param>
/// <returns></returns>
public static bool IsNumber(this string objStr)
{
try
{
return Regex.IsMatch(objStr, @"^\d+(\.\d+)?$");
}
catch
{
return false;
}
} /// <summary>
/// 是否是浮点数
/// </summary>
/// <param name="objStr">输入字符串</param>
/// <returns></returns>
public static bool IsDecimal(this string objStr)
{
try
{
return Regex.IsMatch(objStr, @"^(-?\d+)(\.\d+)?$");
}
catch
{
return false;
}
}
#endregion #endregion #region 业务常用 #region 中文检测
/// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="objStr"></param>
/// <returns></returns>
public static bool IsZhCN(this string objStr)
{
try
{
return Regex.IsMatch(objStr, "[\u4e00-\u9fa5]");
}
catch
{
return false;
}
}
#endregion #region 邮箱验证
/// <summary>
/// 判断邮箱地址是否正确
/// </summary>
/// <param name="objStr"></param>
/// <returns></returns>
public static bool IsEmail(this string objStr)
{
try
{
return Regex.IsMatch(objStr, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
catch
{
return false;
}
}
#endregion #region IP系列验证
/// <summary>
/// 是否为ip
/// </summary>
/// <param name="objStr"></param>
/// <returns></returns>
public static bool IsIP(this string objStr)
{
return Regex.IsMatch(objStr, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
} /// <summary>
/// 判断输入的字符串是否是表示一个IP地址
/// </summary>
/// <param name="objStr">被比较的字符串</param>
/// <returns>是IP地址则为True</returns>
public static bool IsIPv4(this string objStr)
{
string[] IPs = objStr.Split('.');
for (int i = ; i < IPs.Length; i++)
{
if (!Regex.IsMatch(IPs[i], @"^\d+$"))
{
return false;
}
if (Convert.ToUInt16(IPs[i]) > )
{
return false;
}
}
return true;
} /// <summary>
/// 判断输入的字符串是否是合法的IPV6 地址
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsIPV6(string input)
{
string temp = input;
string[] strs = temp.Split(':');
if (strs.Length > )
{
return false;
}
int count = input.GetStrCount("::");
if (count > )
{
return false;
}
else if (count == )
{
return Regex.IsMatch(input, @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$");
}
else
{
return Regex.IsMatch(input, @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$");
}
}
#endregion #region 网址系列验证
/// <summary>
/// 验证网址是否正确(http:或者https:)【后期添加 // 的情况】
/// </summary>
/// <param name="objStr">地址</param>
/// <returns></returns>
public static bool IsWebUrl(this string objStr)
{
try
{
return Regex.IsMatch(objStr, @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?|https://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
}
catch
{
return false;
}
} /// <summary>
/// 判断输入的字符串是否是一个超链接
/// </summary>
/// <param name="objStr"></param>
/// <returns></returns>
public static bool IsURL(this string objStr)
{
string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
return Regex.IsMatch(objStr, pattern);
}
#endregion #region 邮政编码验证
/// <summary>
/// 验证邮政编码是否正确
/// </summary>
/// <param name="objStr">输入字符串</param>
/// <returns></returns>
public static bool IsZipCode(this string objStr)
{
try
{
return Regex.IsMatch(objStr, @"\d{6}");
}
catch
{
return false;
}
}
#endregion #region 电话+手机验证
/// <summary>
/// 验证手机号是否正确
/// </summary>
/// <param name="objStr">手机号</param>
/// <returns></returns>
public static bool IsMobile(this string objStr)
{
try
{
return Regex.IsMatch(objStr, @"^13[0-9]{9}|15[012356789][0-9]{8}|18[0123456789][0-9]{8}|147[0-9]{8}$");
}
catch
{
return false;
}
} /// <summary>
/// 匹配3位或4位区号的电话号码,其中区号可以用小括号括起来,也可以不用,区号与本地号间可以用连字号或空格间隔,也可以没有间隔
/// </summary>
/// <param name="objStr"></param>
/// <returns></returns>
public static bool IsPhone(this string objStr)
{
try
{
return Regex.IsMatch(objStr, "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$");
}
catch
{
return false;
}
}
#endregion #region 字母或数字验证
/// <summary>
/// 是否只是字母或数字
/// </summary>
/// <param name="objStr"></param>
/// <returns></returns>
public static bool IsAbcOr123(this string objStr)
{
try
{
return Regex.IsMatch(objStr, @"^[0-9a-zA-Z\$]+$");
}
catch
{
return false;
}
}
#endregion #endregion
}

说这么多基本上差不多完了,下次列表+字典+元组就一起讲了啊~

Python3 与 NetCore 基础语法对比(String专栏)的更多相关文章

  1. Python3 与 C# 面向对象之~继承与多态 Python3 与 C# 面向对象之~封装 Python3 与 NetCore 基础语法对比(Function专栏) [C#]C#时间日期操作 [C#]C#中字符串的操作 [ASP.NET]NTKO插件使用常见问题 我对C#的认知。

    Python3 与 C# 面向对象之-继承与多态   文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html 目录: 2.继承 ¶ 2.1.单继 ...

  2. Python3 与 NetCore 基础语法对比(Function专栏)

    Jupyter最新排版:https://www.cnblogs.com/dotnetcrazy/p/9175950.html 昨晚开始写大纲做demo,今天牺牲中午休息时间码文一篇,希望大家点点赞 O ...

  3. Python3 与 NetCore 基础语法对比(就当Python和C#基础的普及吧)

    Jupyter排版:https://www.cnblogs.com/dotnetcrazy/p/9102030.html 汇总系列:https://www.cnblogs.com/dunitian/p ...

  4. Python3 与 NetCore 基础语法对比(List、Tuple、Dict、Set专栏)

    Jupyter最新版:https://www.cnblogs.com/dotnetcrazy/p/9155310.html 在线演示:http://nbviewer.jupyter.org/githu ...

  5. Python3 与 C# 基础语法对比(Function专栏)

      Code:https://github.com/lotapp/BaseCode 多图旧版:https://www.cnblogs.com/dunitian/p/9186561.html 在线编程: ...

  6. Python3 与 C# 基础语法对比(就当Python和C#基础的普及吧)

      文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html 多图旧排版:https://www.cnblogs.com/dunitian/p/9 ...

  7. Python3 与 C# 基础语法对比(String专栏)

      Code:https://github.com/lotapp/BaseCode 多图旧排版:https://www.cnblogs.com/dunitian/p/9119986.html 在线编程 ...

  8. Python3 与 C# 基础语法对比(List、Tuple、Dict、Set专栏)

      Code:https://github.com/lotapp/BaseCode 多图旧版:https://www.cnblogs.com/dunitian/p/9156097.html 在线预览: ...

  9. python3笔记<一>基础语法

    随着AI人工智能的兴起,网络安全的普及,不论是网络安全工程师还是AI人工智能工程师,都选择了Python.(所以本菜也来开始上手Python) Python作为当下流行的脚本语言,其能力不言而喻,跨平 ...

随机推荐

  1. rxjs简单入门

    rxjs全名Reactive Extensions for JavaScript,Javascript的响应式扩展, 响应式的思路是把随时间不断变化的数据.状态.事件等等转成可被观察的序列(Obser ...

  2. 2n的 位数

    len=())+,(2n−1同样适用)

  3. 爬虫3 requests基础2 代理 证书 重定向 响应时间

    import requests # 代理 # proxy = { # 'http':'http://182.61.29.114.6868' # } # res = requests.get('http ...

  4. window下配置SSH连接GitHub、GitHub配置ssh key

    window下配置SSH连接GitHub.GitHub配置ssh key   此经验分两部分: 第一部分介绍:在windows下通过msysGit(Git for windows.Git Bash)配 ...

  5. HDU 4135 Co-prime (容斥+分解质因子)

    <题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. ...

  6. HDU 1522 Marriage is Stable 【稳定婚姻匹配】(模板题)

    <题目链接> 题目大意: 给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配. 解题分析: 稳定婚姻问题的 ...

  7. 002.MySQL高可用主从复制部署

    一 基础环境 主机名 系统版本 MySQL版本 主机IP master CentOS 6.8 MySQL 5.6 172.24.8.10 slave01 CentOS 6.8 MySQL 5.6 17 ...

  8. redis初步入门(2)

    一.redis持久化 1.redis是一个内存数据库,当redis服务器重启,或者电脑关机重启,数据会丢失,所以需要将redis内存中的数据持久化保存到硬盘文件中. 2.redis持久化机制 (1)R ...

  9. 利用shell脚本实现nginx 的logs日志分割

    Nginx 是一个非常轻量的 Web 服务器,体积小.性能高.速度快等诸多优点.但不足的是也存在缺点,比如其产生的访问日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将 导致日志文件容量 ...

  10. 使用Admin监控

    在springboot中,也提供了很全面的监控系统.这篇文章介绍一下springboot-admin监控springboot项目. 原来大致是这样的,springboot--admin--server ...