• 【dart 语法】String 介绍


    字符串定义
    使用单引号或双引号
    
    String a = "abcdefg";
    String b = '12345';
    创建多行字符串,保留内在格式
    使用三个单引号或三个双引号 创建多行字符串,保留内在格式,如换行和缩进等,里面写什么输出就是什么。
    三个单引号
        String e = '''asd
         fdsd
           fff
        
        ''';
    三个双引号 
    String f = """ 1
        2
        3
        4
        """;
    使用r创建原始raw字符串(转义字符等特殊字符会输出出来,而不会自动被转义)
    String str1=r'Hello 
      World'   //(使用r创建原始字符串,转义字符不会被转义,会直接输出出来)
    字符串常用属性
    
     String a20 = "aaaa";
     String a21 = "";
    字符串长度
    
    print(a20.length);//4
    是否为空
    
    print(a20.isEmpty);//false 
    print(a21.isEmpty);//true
    是否不为空
    
    print(a20.isNotEmpty);//true 
    print(a21.isNotEmpty);//false
    字符串连接
    
    String a = "abcdefg";
    String b = '12345';
    使用+号连接
    
    String c = a + b; //使用+号连接
    print(c); //abcdefg12345
    使用相邻空格符号连接,必须是两个字符串 不能是变量
    
    String d = 'aaa' 'bbb'; //使用相邻空格符号连接,必须是两个字符串 不能是变量 
    print(d); //aaabbb 
    字符串模板,使用${} 将一个字符串变量/表达式嵌入到另一个字符串内
    
    变量
    String a1 = "aa";
    String b1 = "bb${a1}bb";
    print(b1); //bbaabb
    表达式
    
     String a1 = "aa";
     String b2 = "bb${a1.toUpperCase()}bb";
     print(b2); //bbAAbb
    字符串与数字之间的转换
    
    字符串转int数值
    int int1 = int.parse("33");
    print(int1); //33
    字符串转double数值
    double d1 = double.parse("3.33");
    print(d1); //3.33
    数值转字符串
    print(33.toString());
    print(3.34.toString());
    数值转字符串保留精度
    print(3.12345.toStringAsFixed(3)); //保留精度 3.123
    字符串切割
    String a2 = "aaabbb";
      
    含头不含尾
    print(a2.substring(0, 2)); //aa 含头不含尾
    从指定index至末尾
    print(a2.substring(3)); //bbb 从指定index至末尾
    使用,分割,返回的是一个数组
    String a5 = "a,d,d d,c,,"; 
    List<String> a6 = a5.split(",");//使用,分割,返回的是一个数组
    print(a6.length); //6 
    print(a6);//[a, d, d d, c, , ];
    正则匹配,拆分字符串
    String strB = "abba";
    print(strB.split(new RegExp(r"b*")));
    查询,并替换
    复制代码
     String a8 = "a b,c";
        String a7 = a8.splitMapJoin(",",//查询“,”,用onMatch的返回值替换“,”用onNonMatch的返回值替换其他
            onMatch: (Match match) {
              return "a";
            }, onNonMatch: (String nonMatch) {
              return "b";
            });
        print(a7);//bab    a b,c  =>   bab
    复制代码
    字符串判断 是否包含或以xxx开始结束等
    
    String a3 = "aaabbbccc";
    是否以‘xxx’开头
    print(a3.startsWith("aa")); //true
    print(a3.startsWith("aa", 3)); //false 从index=3开始判断
    是否以‘xxx’结尾
    print(a3.endsWith("c")); //true
    是否包含‘xxx’
    
    print(a3.contains("ab")); //true
    print(a3.contains("ac")); //false
    从某处开始是否包含‘xxx’
    
    print(a3.contains("ab", 3)); //false 从index=3开始判断
    字符串替换
    String a4 = "abcdeab";
    
    替换全部符合条件的
    print(a4.replaceAll("ab","cc"));//cccdecc 替换全部符合条件的
    只替换第一个符合条件的
    print(a4.replaceFirst("ab", "dd"));//ddcdeab 只替换第一个符合条件的
    从index=3开始 替换第一个符合条件的
    print(a4.replaceFirst("ab", "dd",3));//abcdedd 从index=3开始 替换第一个符合条件的
    范围替换 从0-3 含0不含3
    print(a4.replaceRange(1, 3, "z"));// 范围替换 从0-3 含0不含3
    用方法返回值替换指定的字符串
    print(a4.replaceAllMapped("c", (Match match){//abyydeab 用方法返回值替换指定的字符串
      return "yy";
    }));
    从index=2开始 用方法返回值替换指定的字符串
    print(a4.replaceFirstMapped("b", (Match match){//abcdea333 从index=2开始 用方法返回值替换指定的字符串
      return "333";
    },2));
    字符串查找
    String a9 = "aababcc1bc23";
    获取指定字符出现的位置
    复制代码
    String str = 'Dartisans';
    
    print(str.indexOf('art'));
    print(str.indexOf(new RegExp(r'[A-Z][a-z]')));
    
    print(str.lastIndexOf('a'));
    print(str.lastIndexOf(new RegExp(r'a(r|n)')));
    复制代码
    第一个符合条件的index
    print(a9.indexOf("ab"));//1 
    从index=xx开始往后找
    print(a9.indexOf("ab",2));//3 从index=2开始往后找
    print(a9.indexOf("ab",4));//-1 从index=4开始往后找,找不到返回-1
    从后往前找 返回第一个符合条件的index
    print(a9.lastIndexOf("bc"));//8 从后往前找 返回第一个符合条件的index
    从后往前找 从index=xxx开始找 返回第一个符合条件的index 找不到返回-1
    print(a9.lastIndexOf("bc",3));//-1 从后往前找 从index=3开始找 返回第一个符合条件的index 找不到返回-1
    
    print(a9.lastIndexOf("bc",7));//4 从后往前找 从index=7开始找 返回第一个符合条件的index
    
    转换为大小写
    String a10 = "aaBBCc";
    大写转小写 
    
    print(a10.toLowerCase());//aabbcc
    小写转大写
    
    print(a10.toUpperCase());//AABBCC
    去除空格
    String a11 = " aab bcc ";
    print(a11);// aab bcc
    去除左右两边空格
    
    print(a11.trim());//aab bcc 去除左右两边空格
    去除右边空格
    
    print(a11.trimRight());// aab bcc 去除右边空格
    去除左边空格
    
    print(a11.trimLeft());// aab bcc //去除左边空格
    补齐长度 剩余位使用指定字符串替换
    String a13 = "111";
    剩余3个位 默认使用""补齐
    
    print(a13.padLeft(6));// 111 剩余3个位 默认使用""补齐
    剩余3个位 指定使用"c
    
    print(a13.padRight(6,"c")); //111ccc 剩余3个位 指定使用"c"
    剩余3个位 每个位指定使用"dd" 替换后总长度不是6
    
    print(a13.padRight(6,"dd")); //111dddddd 剩余3个位 每个位指定使用"dd" 替换后总长度不是6
    如果指定长度小于原字符串长度 返回原字符串
    
    print(a13.padLeft(2,"e"));//111 如果指定长度小于原字符串长度 返回原字符串
    字符串先后比较
    String a12 = "bbcc";
    print(a12.compareTo("aaa"));//1 在ascii码中 b>a
    print(a12.compareTo("bbcc"));//0
    print(a12.compareTo("dd"));//-1 在ascii码中 b<d
    字符串Unicode编码
    codeUnitAt(方法返回给定索引处的16位UTF-16代码单元) 
    
    语法
    String.codeUnitAt(int index)
    参数
      index- 表示字符串中的字符索引。
    返回值
      返回一个数字值。
    示例
    void main() { 
       var res = "Good Day"; 
       print("Code Unit of index 0 (G): ${res.codeUnitAt(0)}");  
    }
    执行上面示例代码,得到以下结果 - 
    
    Code Unit of index 0 (G): 71
    codeUnits(属性返回给定字符串的UTF-16代码单元列表)
    语法
    String.codeUnits
    示例
    void main() { String str = "Hello"; print(str.codeUnits); }
    执行上面示例代码,得到以下结果 - 
    
    [72, 101, 108, 108, 111]
    字符串运算符(+、==、[])
    +:加好运算符,字符串拼接功能
    
    String a = 'abc';
    
    String b = 'cbd';
    
    print(a+b);//abccbd
    
    ==:等号运算符,比较两个字符串是否相同
    
    复制代码
    String a = 'abc';
    String b = '2';
    if(a == b){
     print('true');
    }else{
     print('false');
    }//返回false
    
    复制代码
    []:取值运算符,取出字符串索引位指向的单个字符
    
    String a = 'abc';
    
    String b = '2'; print(a[1]);//b
    
    字符串变量
    StringBuffer是可改变的,定义后还可以再修改
    
    StringBuffer xiaomingSaid = new StringBuffer();
    xiaomingSaid.write("All the world's a stage ... ");
    xiaomingSaid.write("And all the men and women merely players ...");
    print(xiaomingSaid);
    部分属性
    
    StringBuffer strBuf = new StringBuffer();
    strBuf.write("Sow nothing, reap nothing.");
    
    返回字符串缓冲区的哈希码
    
    print(strBuf.hashCode);
    字符串缓冲区是否为空
    
    print(strBuf.isEmpty);
    字符串缓冲区是否不为空
    
    print(strBuf.isNotEmpty);
    返回字符串缓冲区累积内容的长度
    
    print(strBuf.length);
    返回对象运行时的类型
    
    print(runtimeType);
     
    
    常用方法
    StringBuffer strBuf = new StringBuffer();
    添加字符串到字符串缓冲区内
    strBuf.write("Do one thing at a time, and do well.");
    返回字符串缓冲区的所有内容
    print(strBuf.toString());
    清除字符串缓冲区
    strBuf.clear();

    dart 近似java

    几个特殊的点:

    1.单引号,双引号通用。

    2.3引号声明多行。

    3.str[index] 角标取值。

    4.“==”双等号运算符,比较两个字符串是否相同。

    5.格式化 使用“${}”  特殊用法:

    '$str' 是一个对象。持有str 的引用。当str改变时。'$str'会随之改变。
  • 相关阅读:
    Python 3基础教程32-正则
    Python 3基础教程31-urllib模块
    Python 3基础教程30-sys模块
    Python 3基础教程29-os模块
    Python 3基础教程28-内置函数
    Python 3基础教程27-字典
    Python 3基础教程26-多行打印
    Python 3基础教程24-读取csv文件
    Python 3基础教程25-异常处理
    Python 3基础教程23-多维列表
  • 原文地址:https://www.cnblogs.com/mamamia/p/13678786.html
Copyright © 2020-2023  润新知