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

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

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

相關推薦

Python3 NetCore 基礎語法對比String專欄

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

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

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

Python3 NetCore 基礎語法對比Function專欄

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

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

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

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 //

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'

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

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

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

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

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

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

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

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

python3第一天(基礎語法)

python3基礎語法對於python3的應用程序,解釋器用/usr/bin/python3(python3如果不在此目錄下,更換到相應目錄).對於運行,可以對寫的腳本添加執行權限,也可以用指定的程序來執行(如:python3 ./腳本)默認情況下,Python3的源碼文件都是utf-8編碼,所有的字符串都是

課後筆記一:Python基礎語法介紹1

Python3.6.5我們總是找借口說還有時間去做某事,或者完成某事,結果總是不了了之。比如拖到現在才寫第一課聽課筆記。 第一節課,老師對以下內容做了分享,大致分為 環境配置 開發平臺:Mac OS Version 10.13.2 老師講課的是在Windows 10 我選擇在Mac OS進行Pyth

java基礎學習總結十九:UnsafeCAS

Unsafe         簡單講一下這個類。Java無法直接訪問底層作業系統,而是通過本地(native)方法來訪問。不過儘管如此,JVM還是開了一個後門,JDK中有一個類Unsafe,它提供了硬體級別的原子操作。     &n

java基礎學習總結十五:抽象類介面

       抽象類與介面是java語言中對抽象概念進行定義的兩種機制,正是由於他們的存在才賦予java強大的面向物件的能力。他們兩者之間對抽象概念的支援有很大的相似,甚至可以互換,但是也有區別。  一、抽象類    &n

Python3 教程 - 001 基礎語法

Python3 基礎語法 編碼 預設情況下,Python 3 原始碼檔案以 UTF-8 編碼,所有字串都是 unicode 字串。當然你也可以為原始碼檔案指定不同的編碼: # -*- coding: cp-1252 -*- 上述定義允許在原始檔中使用 Windows-1252 字符集中的

手繪碼繪的對比靜態

在這次對比用程式碼和用手繪創作靜態繪畫的實驗中,選擇的程式設計工具是p5.js,p5.js語法和使用上較為簡單,入門很快,繪製的作品為一個簡單的卡通小黃人。下面按要求從思路、技術、創作體驗、創作偏好等方面比較一下二者,以及它們的異同。 碼繪程式碼如下: function setup()

2019年 Selenium3Python3實戰Web自動化測試框架最新50G

第1章 課程介紹本章對課程做整體介紹,通過講解web自動化測試需要掌握的知識,到web自動化測試框架的選擇,框架的搭建。1-1 課程介紹 第2章 環境搭建本章講解自動化測試環境的搭建,並通過實際專案實戰講解selenium3的基礎知識,對常用的api進行徹底的分析、設計,再到如何去編寫自動化測試指令碼。2-

Atitit 演算法原理導論 目錄 1. Attilax總結的有用演算法 按用途分類 1 1.1. 排序演算法 字串匹配String Matching 1 1.2. 加密演算法 編碼演算法 序列

Atitit 演算法原理與導論   目錄 1. Attilax總結的有用演算法 按用途分類 1 1.1. 排序演算法  字串匹配(String Matching) 1 1.2. 加密演算法  編碼演算法  序列化演算法 1 1.3. 查