• 多语言的字符串处理记录


    各个语言的字符串处理记录。

    目的

    日常中会用到很多语言:比如工作中常用java,python,javascript;家里常用go,python,rust;
    偶尔也会折腾下c/c++。
    汇总下各个语言的字符串处理,方便查找。
    不管用什么语言开发,总是会遇到大量的字符串处理,比如字符串split,substring等等。

    模板

    获取字符串的第一个字母
    获取字符串的第二和第三个字母
    获取字符串的最后一个字母
    字符串开头字母判断
    字符串结尾字母判断
    获取字符串长度
    大小写转换
    字符串转int,int转字符串
    分割字符串
    判断字符串是否包含
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    程序实现

    java

    public class Java_String {
    	public static void main(String [] args){
    		String str="Hello,World";
    		//1.获取字符串的第一个字母
    		String s = 	str.substring(0,1);
    		System.out.println("1.获取字符串的第一个字母:"+s);	
    
    		//2.获取字符串的第二和第三个字母
    		s=str.substring(1,3);
    		System.out.println("2.获取字符串的第二和第三个字母:"+s);
    
    		//3.获取字符串的最后一个字母
    		s=str.substring(str.length()-1);
    		System.out.println("3.获取字符串的最后一个字母:"+s);
    
    		//4.字符串开头字母判断
    		if(str.startsWith("Hello")){
    			System.out.println("4.字符串开头字母比较:以Hello开头");
    		}
    
    		//5.字符串结尾字母判断
    		if(str.endsWith("World")){
    			System.out.println("5.字符串结尾字母比较:以World结尾");
    		}
    
    		//6.获取字符串长度
    		System.out.println("6.获取字符串长度:"+str.length());
    
    		//7.大小写转换
    		System.out.println("7.大小写转换:"+str.toUpperCase()+","+str.toLowerCase());
    
    		//8.字符串转int,int转字符串
    		System.out.println("8.字符串转int,int转字符串:"+Integer.parseInt("100")+","+String.valueOf(100));
    
    		//9.分割字符串
    		String[] strs = str.split(",");
    		System.out.println("9.分割字符串:"+"["+strs[0]+","+strs[1]+"]");
    
    		//10.判断字符串是否包含
    		if(str.contains("o,W")){
    			System.out.println("10.分割字符串:包含o,W");
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    go

    package main
    import (
        "fmt"
        "strings"
        "strconv"
    )
    func main() {
    	var str string = "Hello,World"
    	//1.获取字符串的第一个字母
    	fmt.Printf("1.获取字符串的第一个字母:%c\n", str[0])
    
    	//2.获取字符串的第二和第三个字母
    	fmt.Printf("2.获取字符串的第二和第三个字母:%s\n", str[1:3])
    
    	//3.获取字符串的最后一个字母
    	fmt.Printf("3.获取字符串的最后一个字母:%c\n", str[len(str)-1])
    
    	//4.字符串开头字母判断
    	if strings.HasPrefix(str,"Hello") {
    		fmt.Printf("4.字符串开头字母比较:以Hello开头\n")
    	}
    
    	//5.字符串结尾字母判断
    	if strings.HasSuffix(str,"World") {
    		fmt.Printf("5.字符串结尾字母比较:以World结尾\n")
    	}
    
    	//6.获取字符串长度
    	fmt.Printf("6.获取字符串长度:%d\n", len(str))
    
    	//7.大小写转换
    	fmt.Printf("7.大小写转换:%s,%s\n",strings.ToUpper(str),strings.ToLower(str))
    	//8.字符串转int,int转字符串
    	intStr, _ := strconv.Atoi("100")
    	fmt.Printf("8.字符串转int,int转字符串:%d,%s\n",intStr,strconv.Itoa(100))
    	//9.分割字符串
    	var strs []string = strings.Split(str,",")
    	fmt.Printf("9.分割字符串:[%s,%s]\n",strs[0],strs[1])
    	//10.判断字符串是否包含
    	if strings.Index(str,"o,W")!=-1 {
    		fmt.Printf("10.分割字符串:包含o,W\n");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    python

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    _str="Hello,World"
    
    #1.获取字符串的第一个字母
    print "1.获取字符串的第一个字母:"+_str[0]
    
    #2.获取字符串的第二和第三个字母
    print "2.获取字符串的第二和第三个字母:"+_str[1:3]
    
    #3.获取字符串的最后一个字母
    print "3.获取字符串的最后一个字母:"+_str[-1]
    
    #4.字符串开头字母判断
    if _str.startswith('Hello'):
    	print "4.字符串开头字母比较:以Hello开头"
    
    #5.字符串结尾字母判断
    if _str.endswith('World'):
    	print "5.字符串结尾字母比较:以World结尾"
    
    #6.获取字符串长度
    print "6.获取字符串长度:"+str(len(_str))
    
    #7.大小写转换
    print "7.大小写转换:"+_str.upper()+","+_str.lower()
    
    #8.字符串转int,int转字符串
    print "8.字符串转int,int转字符串:"+str(int("100"))+","+str(100)
    
    #9.分割字符串
    print "9.分割字符串:["+_str.split(',')[0]+","+_str.split(',')[1]+"]"
    
    #10.判断字符串是否包含
    if _str.find('o,W')!=-1:
    	print "10.分割字符串:包含o,W"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    cpp

    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    vector<string> split(const string& src, const string sp) ;
    int main() {
    	string str("Hello,World");
    	//1.获取字符串的第一个字母
    	cout << "1.获取字符串的第一个字母:" + str.substr(0, 1) << endl;
    	//2.获取字符串的第二和第三个字母
    	cout << "2.获取字符串的第二和第三个字母:" + str.substr(1, 2) << endl;
    	//3.获取字符串的最后一个字母
    	cout << "3.获取字符串的最后一个字母:" + str.substr(str.length() - 1, 1) << endl;
    	//4.字符串开头字母判断
    	if (str.find("Hello") == 0) {
    		cout << "4.字符串开头字母比较:以Hello开头" << endl;
    	}
    	//5.字符串结尾字母判断
    	string w("World");
    	if (str.rfind(w) == str.length() - w.length()) {
    		cout << "5.字符串结尾字母比较:以World结尾" << endl;
    	}
    	//6.获取字符串长度
    	stringstream ss;
    	ss << str.length();
    	cout << "6.获取字符串长度:" + ss.str() << endl;
    	//7.大小写转换
    	transform(str.begin(), str.end(), str.begin(), ::toupper);
    	cout << "7.大小写转换:" + str;
    	transform(str.begin(), str.end(), str.begin(), ::tolower);
    	cout << "," + str << endl;
    	//8.字符串转int,int转字符串
    	int num;
    	stringstream ss2("100");
    	ss2 >> num;
    	stringstream ss3;
    	ss3 << num;
    	cout << "8.字符串转int,int转字符串:" + ss3.str() + "," + ss3.str() << endl;
    	//9.分割字符串
    	vector<string> strs = ::split(str,string(","));
    	cout << "9.分割字符串:[" + strs[0] +","+strs[1]<<"]"<<endl;
    	//10.判断字符串是否包含
    	str="Hello,World";
    	if (str.find("o,W")!=-1) {
    		cout << "10.分割字符串:包含o,W" << endl;
    	}
    	return 0;
    }
    vector<string> split(const string& src, string sp) {
    	vector<string> strs;
    	int sp_len = sp.size();
    	int position = 0, index = -1;
    	while (-1 != (index = src.find(sp, position))) {
    		strs.push_back(src.substr(position, index - position));
    		position = index + sp_len;
    	}
    	string lastStr = src.substr(position);
    	if (!lastStr.empty())
    		strs.push_back(lastStr);
    	return strs;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    php

     
    $_str="Hello,World";
    
    //1.获取字符串的第一个字母
    echo "1.获取字符串的第一个字母:".substr($_str,0,1)."
    "
    ; //2.获取字符串的第二和第三个字母 echo "2.获取字符串的第二和第三个字母:".substr($_str,1,2)."
    "
    ; //3.获取字符串的最后一个字母 echo "3.获取字符串的最后一个字母:".substr($_str,strlen($_str)-1,1)."
    "
    ; //4.字符串开头字母判断 if(strrpos($_str,"Hello")!==false && strrpos($_str,"Hello")==0){ echo "4.字符串开头字母比较:以Hello开头
    "
    ; } //5.字符串结尾字母判断 if(strrpos($_str,"World")!==false && strrpos($_str,"World")== strlen($_str)-5){ echo "5.字符串结尾字母比较:以World结尾
    "
    ; } //6.获取字符串长度 echo "6.获取字符串长度:".strlen($_str)."
    "
    ; //7.大小写转换 echo "7.大小写转换:".strtoupper($_str).",".strtolower($_str)."
    "
    ; //8.字符串转int,int转字符串 echo "8.字符串转int,int转字符串:".intval("100").",".((string)100)."
    "
    ; //9.分割字符串 $a = explode(',',$_str); echo "9.分割字符串:[".$_str."--->".$a[0]."]
    "
    ; //10.判断字符串是否包含 if(strrpos($_str,"o,W")!==false && strrpos($_str,"o,W")>=0){ echo "10.分割字符串:包含o,W
    "
    ; } ?>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    【Linux】中安装pip(详细教程)
    企业数字化转型时,会遇到的5大挑战
    《面试八股文》之Dubbo17卷
    1.什么是软件工程?它目标和内容是什么?2.软件开发中有哪几种过程模型?哪些适用于面向对象的软件开发?
    Docker之Harbor
    MySQL的指令大全和注意事项(强烈推荐收藏)
    系统架构设计师(第二版)学习笔记----系统工程
    centos7 安装 RabbitMq
    Compose在xml中使用滑动冲突处理
    Ceph 在Linux上的使用
  • 原文地址:https://blog.csdn.net/yuoveyu/article/details/127692090