3. Strings

3.1 string creation

1. String definition

Use single or double quotation marks

String a = "abcdefg";
String b = '12345';
Copy the code

2. Create a multi-line string with built-in formatting

Use three single quotes or three double quotes to create a multi-line string with built-in formatting, such as newlines and indentation, and output whatever is written inside.

  • Three single quotes
String e = '''asd
     fdsd
       fff
    
    ''';
Copy the code
  • Three double quotes
String f = """ 1
    2
    3
    4
    """;
Copy the code

3, create raw string with r (special characters such as escape characters will be printed, not automatically escaped)

String str1=r'Hello \n  World'   
Copy the code

3.2. Common string attributes

1. General attributes

String a20 = "aaaa";
String a21 = "";
Copy the code
  • 1. The length of the string
print(a20.length); / / 4Copy the code
  • 2. Whether it is empty
print(a20.isEmpty); //false print(a21.isEmpty); //trueCopy the code
  • 3, whether is not empty
print(a20.isNotEmpty); //true print(a21.isNotEmpty); //falseCopy the code

2. String concatenation

String a = "abcdefg";
String b = '12345';
Copy the code
  • 1. Use the + sign to connect
String c = a + b; Print (c); //abcdefg12345Copy the code
  • 2, use adjacent space character concatenation, must be two strings cannot be variables
String d = 'aaa' 'bbb'; Print (d); print(d); //aaabbbCopy the code

String template, use ${} to embed a string variable/expression into another string

  • 1, variables,
String a1 = "aa";
String b1 = "bb${a1}bb";
print(b1); //bbaabb
Copy the code
  • 2. Expressions
String a1 = "aa";
String b2 = "bb${a1.toUpperCase()}bb";
print(b2); //bbAAbb
Copy the code

4. Conversion between strings and numbers

  • 1, string to int value
int int1 = int.parse("33"); print(int1); / / 33Copy the code
  • 2. String to double
Double d1 = double. Parse (" 3.33 "); print(d1); / / 3.33Copy the code
  • 3. Value to string
print(33.toString()); Print (3.34. The toString ());Copy the code
  • 4, the value of the string to retain precision
Print (3.12345 toStringAsFixed (3)); // Retain accuracy 3.123Copy the code

5. String cutting

String a2 = "aaabbb";
Copy the code
  • 1, including the head without the tail
print(a2.substring(0, 2)); //aa includes the head but not the tailCopy the code
  • 2, from the specified index to the end
print(a2.substring(3)); // BBB from index to endCopy the code
  • 3. Split returns an array
String a5 = "a,d,d d,c,,"; List<String> a6 = a5.split(","); Print (a6.length); print(a6.length); / / 6 print (a6); //[a, d, d d, c,];Copy the code
  • 4, regular matching, split string
String strB = "abba";
print(strB.split(new RegExp(r"b*")));
Copy the code
  • 5. Query and replace
String a8 = "a b,c"; String a7 = a8.splitMapJoin(",",// Query ",", replace "with the return value of onMatch," replace other onmatches with the return value of onNonMatch: (Match Match) {return "a"; }, onNonMatch: (String nonMatch) { return "b"; }); print(a7); //bab a b,c => babCopy the code

6. Check whether the string contains or ends with XXX

String a3 = "aaabbbccc";
Copy the code
  • 1. Whether to start with ‘XXX’
print(a3.startsWith("aa")); //true print(a3.startsWith("aa", 3)); //false Start with index=3Copy the code
  • 2, whether to end with ‘XXX’
print(a3.endsWith("c")); //true
Copy the code
  • 3. Whether to include ‘XXX’
print(a3.contains("ab")); //true
print(a3.contains("ac")); //false
Copy the code
  • 4. Whether to include ‘XXX’ from somewhere
print(a3.contains("ab", 3)); //false Start with index=3Copy the code

7. String replacement

String a4 = "abcdeab";
Copy the code
  • Replace all of them
print(a4.replaceAll("ab","cc")); // replace all qualified cccdeccCopy the code
  • Replace only the first one that meets the criteria
print(a4.replaceFirst("ab", "dd")); //ddcdeab replaces only the first one that meets the conditionCopy the code
  • Replace the first one that meets the criteria starting at index=3
print(a4.replaceFirst("ab", "dd",3)); //abcdedd replaces the first qualified index with index=3Copy the code
  • Range of substitution from 0 to 3 including 0 without 3
print(a4.replaceRange(1, 3, "z")); // Replace the range from 0 to 3 with 0 but not 3Copy the code
  • Replaces the specified string with the method return value
Print (a4. ReplaceAllMapped ("c", (Match Match){//abyydeab replace the specified string with a method return value return "yy"; }));Copy the code
  • Replaces the specified string with the method return value starting with index=2
// abcDEA333 replace the specified string with the method return value from index=2 print(a4.replaceFirstMapped("b", (Match Match){return "333"; }, 2));Copy the code

8. String lookup

String a9 = "aababcc1bc23";
Copy the code
  • Gets the position at which the specified character appears
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)')));
Copy the code
  • The first index that meets the criteria
print(a9.indexOf("ab")); / / 1Copy the code
  • Index =xx
print(a9.indexOf("ab",2)); Print (a9.indexof ("ab",4)); print(a9.indexof ("ab",4)); // index=4; // index=4Copy the code
  • Looking backwards returns the first index that matches the criteria
print(a9.lastIndexOf("bc")); //8 Returns the first index that matches the condition from back to frontCopy the code
  • Start with index= XXX and return -1 if no index is found
Print (a9.lastindexof (" BC ",3)); print(a9.lastindexof (" BC ",3));Copy the code

// start index=7 and return the first index that meets the condition

print(a9.lastIndexOf("bc",7));
Copy the code

9. Remove whitespace

String a11 = " aab bcc "; print(a11); // aab bccCopy the code
  • Remove the left and right Spaces
print(a11.trim()); //aab BCC remove the left and right SpacesCopy the code
  • Remove the space on the right
print(a11.trimRight()); // aab BCC removes the right spaceCopy the code
  • Remove the left space
print(a11.trimLeft()); // aab BCC // remove left SpacesCopy the code

10. Replace the remaining bits of the completion length with the specified string

String a13 = "111";
Copy the code
  • The remaining three bits are complemented by “” by default
print(a13.padLeft(6)); // the remaining three bits are completed by defaultCopy the code
  • The remaining three bits specify “c”
print(a13.padRight(6,"c")); // 111CCC specifies the remaining 3 bits to use "C"Copy the code
  • The remaining three bits each specify that the total length is not 6 when replaced with “dd”
print(a13.padRight(6,"dd")); // 111DDDDDD three bits each bit specifies that the total length is not 6 after "dd" is usedCopy the code
  • Returns the original string if the specified length is less than the original string length
print(a13.padLeft(2,"e")); //111 Returns the original string if the specified length is less than the original string lengthCopy the code

11. String sequence comparison

String a12 = "bbcc"; print(a12.compareTo("aaa")); B >a print(a12.compareTo(" BBCC ")); //0 print(a12.compareTo("dd")); //-1 b<d in ASCIICopy the code

12. Unicode encoding of strings

  • CodeUnitAt (method returns a 16-bit utf-16 codeUnitAt a given index)
    • The index- argument represents the character index in the string.
    • Return value Returns a numeric value.
String.codeUnitAt(int index)
void main() { 
   var res = "Good Day"; 
   print("Code Unit of index 0 (G): ${res.codeUnitAt(0)}");  
}
Code Unit of index 0 (G): 71
Copy the code
  • CodeUnits (property returns a list of utf-16 codeUnits for the given string)
String.codeUnits
void main() { String str = "Hello"; print(str.codeUnits); }
 
[72, 101, 108, 108, 111]
Copy the code

String operators (+, ==, [])

  • + : add operator, string concatenation function
String a = 'abc'; String b = 'cbd'; print(a+b); //abccbdCopy the code
  • == : The equal operator that compares whether two strings are the same
String a = 'abc'; String b = '2'; if(a == b){ print('true'); }else{ print('false'); } / / returns falseCopy the code
  • [] : value operator that retrieves a single character pointed to by the string index bit
String a = 'abc'; String b = '2'; print(a[1]); //bCopy the code