1. 程式人生 > >Python3 與 C# 基礎語法對比(String專欄)

Python3 與 C# 基礎語法對比(String專欄)

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() > 0) { 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 = 0; i < IPs.Length; i++) { if (!Regex.IsMatch(IPs[i], @"^\d+$")) { return false; } if (Convert.ToUInt16(IPs[i]) > 255) { 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 > 8) { return false; } int count = input.GetStrCount("::"); if (count > 1) { return false; } else if (count == 0) { 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 C# 基礎語法對比String專欄

using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; public static partial class ValidationHelp

Python3 C# 基礎語法對比Function專欄

主要是普及一下Python基礎語法,對比著Net,新手更容易上手。 對比學習很有意思的,尤其是一些底層和思路 本來感覺函式要說的地方沒多少,細細一規劃,發現~還是單獨拉出一篇說說吧 之後就進入面向物件了,函式還是有必要了解一下的,不然到時候Class裡面的方法定義又要說了。 演示的

Python3 NetCore 基礎語法對比String專欄

using System; using System.Linq; namespace aibaseConsole { public static class Program { private static void Main() {

Python3 NetCore 基礎語法對比Function專欄

using System; using System.Collections.Generic; namespace _6func { class Program { static void Main(string[] args) {

Python3 C# 基礎語法對比List、Tuple、Dict、Set專欄

Help on class set in module builtins: class set(object) | set() -> new empty set object | set(iterable) -> new set object | | Build

Python3 C# 基礎語法對比就當Python和C#基礎的普及吧

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global'

Python3 NetCore 基礎語法對比就當Python和C#基礎的普及吧

歡迎提出更簡單的語法~(文章中案例有兩個福利哦,一個是養生,一個是人工智慧[ 密碼:fqif]) 先說下感覺,python的程式設計有點JavaScript的感覺(比如:'和“有時候不區別),又感覺像外國版的易語言,整個過程像讀書一樣,比如一個元素不在列表之中==> for ite

Python3 NetCore 基礎語法對比List、Tuple、Dict、Set專欄

// using System; // using System.Collections.Generic; // using System.Linq; // namespace aibaseConsole // { // public static class Program //

C. Prefixes and Suffixes string函式

題目連結:https://codeforces.com/contest/1092/problem/C 題意:給你2*n-2的字元子串,每個子串要麼是字首,要麼是字尾,讓你從中猜出來。 題解:我們直接找最長的兩個子串,顯然由這兩個能推出整個串。 #include<cstdio&g

C#基礎:陣列物件陣列

陣列實現位System.Array類的例項,實現了IList,ICollection,IEnumerable介面,陣列一旦建立大小固定,不能新增,刪除元素。  public abstract class Animal//父類,抽象類     {         priv

徹底解決python3python2的版本衝突window版

python版本衝突一直是個大坑,這幾天在研究爬蟲的過程中,整理的一些版本衝突的解決方法如下 在安裝了python3.6之後,設定環境變數path,我的路徑為:C:\Users\lixin\AppD

C++基礎::變數模板variable template

既然允許C++模板類(class template)的存在,允許C++函式模板(function template)的存在,也應當允許變數模板(variable template)的存在。 引入 變數模板,標準C++14(C++11是C++14的

css基礎語法選擇器css導入方式

數字 mic link html clas ref height font 兼容 頁面中,所有的CSS代碼,需要寫入到<style></style>標簽中。style標簽的type屬性應該選擇text/css,但是type屬性可以省略。 CS

【2018.10.11 CC++基礎C Preprocessor的功能及缺陷草稿

repr 繼承 logs 作用域 ces 變量 找到 可變參數 體系 一、前言及參考資料 C Preprocessor即所謂的C預處理器,C++也繼承了C的預處理程序,但在C++語言的設計與演化一書中,C++的設計者Bjarne Strustrup提及他從未喜歡過C預處理器

Python3 C# 網路程式設計之~ 網路基礎

最新版本檢視:https://www.cnblogs.com/dotnetcrazy/p/9919202.html 入門篇 官方文件:https://docs.python.org/3/library/ipc.html(程序間通訊和網路) 例項程式碼:https://github.com/lotapp/

python基礎語法總結-- python類OOP面向物件特性

python常用系統函式方法與模組 python基礎語法總結(一)-- python型別轉換函式+檔案讀寫 python基礎語法總結(二)-- 函式function python基礎語法總結(三)-- 數與字串 python基礎語法總結(四)-- list列表

python基礎語法總結-- 數字串

python常用系統函式方法與模組 python基礎語法總結(一)-- python型別轉換函式+檔案讀寫 python基礎語法總結(二)-- 函式function python基礎語法總結(三)-- 數與字串 python基礎語法總結(四)-- list列表

Python3 C# 擴充套件之~基礎衍生

Help on generator object: test = class generator(object) | Methods defined here: | | __del__(...) | | __getattribute__(self, name, /)

Python3 C# 擴充套件之~基礎拓展

終於期末考試結束了,聰明的小明同學現在當然是美滋滋的過暑假了,左手一隻瓜,右手一本書~正在給老鄉小張同學拓展他研究多日的知識點 1.NetCore裝飾器模式¶ 裝飾器這次從C#開始引入,上次剛講迭代器模式,這次把裝飾器模式也帶一波(純Python方向的可以選擇性跳過,也可以當擴充套件)

C++基礎語法知識點彙總

Const 關鍵字 定義普通變數時,只能初始化一次,不可再修改。 定義指標變數時,在型別前則值不能改,在型別後則地址不能改;若兩者都有,則都不能改。 int a = 2; const int