美文网首页
golang php 和 python常用替换

golang php 和 python常用替换

作者: WayneJoe | 来源:发表于2023-06-27 16:59 被阅读0次

知识点:string array map(dict) json 数字 和 文件; if和循环语句
每周末写点+验证,看似简单,竟也挺费劲
创作不易,分享整理如若有用,辛苦点个免费赞支持下

一 常用字符串操作

1.1 打印输出

// golang
fmt.Println("hello world") // hello world
log.Println("hello world")
// php
echo "hello world";// 字符串
var_dump(array(1,3,4,"hello"));// 所有数据类型
# python3
print("hello world")

1.2 大小写转换

// golang
log.Println(strings.ToLower("Hello World")) // hello world
log.Println(strings.ToUpper("Hello World")) // HELLO WORLD
// php
echo strtolower("Hello World"); 
echo strtoupper("Hello World"); 
# python3
print("hello world".lower())
print("HelloWorld".upper())

1.3 去除空格

// golang
log.Println(strings.TrimSpace("   hello   ")) // hello
// php
echo trim("   hello   ");
# python3
print("   hello   ".strip())

1.4 字符串拼接

// golang
log.Println("hello" + "world") // hello world
// php
echo "hello" . "world";
# python3
print("hello" + "world")
// golang
str := strings.Join( []sting{"ab", "cd", "ef", "g"}, ",") // ab,cd,ef,g
// php
str = implode( ["ab", "cd", "ef", "g"], ',');
# python3
str = ',' . join( ["ab", "cd", "ef", "g"])
str = ',' . join( str(i) for i in [1,2,3]) // 整数需先转字符串

1.5 字符串切割

// golang
arr := strings.Split("ab,cd,ef,g", ",") 
// php
arr = explode("ab,cd,ef,g", ","); // ["ab", "cd", "ef", "g"]
# python3
lst = "ab,cd,ef,g".split(",")

二 数组常用操作(待验证并完善)

2.1 创建和访问

// golang []type{}
slice := []int{7,2,4,9,5}
log.Println(slice[0]) // 7
// php array() 或 []
arr = [4,2,5,1,3];
echo arr[0]; // 4
# python3
lst =  [4,2,5,1,3]
print(lst[0]) # 4

2.2 添加元素

// golang
slice := []string{9,2,5,7}
slice.append(1) // 9,2,5,7,1
// php 
arr = array(9,2,5,7)
arr[] = 1;// 9,2,5,7,1
arr.push(1);// 9,2,5,7,1
arr.shift(4);// 前插入 4, 9,2,5,7
#python3
lst = [9,2,5,7] 
lst.append(1) # 9,2,5,7,1
lst.insert(0, 4) # 前插入,4, 9,2,5,7

2.3 修改元素

// golang
slice := []string{"one", "two"}
slice[1] = "three"
log.Println(slice) // {"one", "three"}
// php
arr = ["one", "two"];
arr[1] = "three";
var_dump(arr);// ["one", "three"]
# python3
lst =  ["one", "two"]
lst[1] = "three"
print(lst)  // ["one", "three"]

2.4 删除元素

// golang
slice := []string{"one", "two"}
delete(slice[0])
// php
arr = ["one", "two"];
unset(arr[0]);
# python3
lst = ["one", "two"]
del lst[0]

2.5 数组长度

// golang
log.Println(len([]int{1,2,3,4})) // 4
// php
echo count([1,2,3,4]); // 4
# python3
print(len([1,2,3,4])) # 4

2.6 数组排序
2.7 数组反转
2.8 数组合并

相关文章

网友评论

      本文标题:golang php 和 python常用替换

      本文链接:https://www.haomeiwen.com/subject/cwjnydtx.html