Dart String Manipulations

Dart String Manipulations

ยท

4 min read

  • Dart String Manipulations means where we can change the string according to to need such we can change their case , we can measure their length , joint the string , remove the word or character and so on.

  • In here I am giving info of some String Manipulations.

Length

  • Length method is use for the measure the length of any string.

  •         const string = 'daRTlang';
            string.length(); //8
    

Substring

  • Substring method use for print string using index.

  • In below example you can see the syntax of substring.

  • (1 , 4) here, 1 is staring index and 4 is the ending index. But remember that index start with 0 and also here compiler print till index 3 not index 4.

  •         const string = 'dartlang';
            var result = string.substring(1); // 'artlang'
            result = string.substring(1, 4); // 'art'
    

Uppercase & Lowecase

  • Like the name , you can guess it that this both methods is use for the change case.

  •         const string = 'daRTlang';
            string.toLowerCase(); //dartlang
            string.toUpperCase(); //DARTLANG
    

Split & Join

  • This method use to separate string and join the string without using any other method like concertation for join string .

      const string = 'Hello world!';
      final splitted = string.split(' ');
      print(splitted); // [Hello, world!];
    
      final result = 'Eats shoots leaves'.splitMapJoin(RegExp(r'shoots'),
          onMatch: (m) => '${m[0]}', // (or no onMatch at all)
          onNonMatch: (n) => '*');
      print(result); // *shoots*
    

    Replacing & Searching

  • This method is use for replace word in the string without change the original string we can simply replace the unwanted word form wanted word.

  •         void main() { 
            String text = "I am a good boy I like milk. Doctor says milk is good for health.";
    
            String newText = text.replaceAll("milk", "water"); 
    
            print("Original Text: $text");
            //I am a good boy I like milk. Doctor says milk is good for health
            print("Replaced Text: $newText"); 
            //I am a good boy I like water. Doctor says milk is good for health 
    
            }
    
  • String Buffer

  • This method use for as a storage , we can create one variable and using write method we can write different type string and Int in the variable and in the last it will print all the string and int value whichever store in message variable , for this we just need to write StringBuffer(); .

  •         void main() { 
            final message = StringBuffer();
                  message.write('Hello');
                  message.write(' my name is ');
                  message.write('Vinit');
                  message.toString();
                  print(message);
            // 'Hello my name is Vinit'
            }
    
  • Building Strings in loop

  •         void main() { 
            for (int i = 2; i <= 20; i *= 2) {
              print(i);
            }
            /** 
            2
            4 
            8
            16
            */
            }
    
  • Strings Validations , Strings startWith & endsWith

  • This method use for validate the value and it give output in only boolean.

  • There are total 3 method to validate .

  • First one is .startsWith();

  • Second one is .endsWith();

  • And the Last one is .contains();

  •         void main() { 
            var text = 'My Name is Vinit Mepani ';
            print(text.startsWith('My')); // true
            print(text.endsWith('Mepani')); // true
            print(text.contains('Name'));    // true
            print(text.contains('isy')); // false
            }
    

    Trimming:

    Trimming involves removing whitespace (spaces, tabs, and newlines) from the beginning and end of a string. Dart provides the trim() method for this purpose.

      void main() {
        String originalString = "   Hello, Dart!   ";
    
        // Trimming leading and trailing whitespace
        String trimmedString = originalString.trim();
        String trimmedStringRight = originalString.trimRight();
        String trimmedStringLeft = originalString.trimLeft();   
    
        print(trimmedString);  // Output: "Hello, Dart!"
        print(trimmedStringRight);  // Output: "   Hello, Dart!"
        print(trimmedStringLeft);  // Output: "Hello, Dart!   "
      }
    

    In this example, trim() removes the leading and trailing whitespaces from the originalString.

    Padding:

    Padding involves adding characters to the beginning or end of a string to achieve a desired length. Dart provides padLeft() and padRight() methods for left and right padding, respectively.

      void main() {
        String originalString = "Dart";
    
        // Padding to the left with "-" to make the length 10
        String leftPaddedString = originalString.padLeft(10, "-");
    
        // Padding to the right with "*" to make the length 10
        String rightPaddedString = originalString.padRight(10, "*");
    
        print(leftPaddedString);   // Output: "------Dart"
        print(rightPaddedString);  // Output: "Dart******"
      }
    

    In this example, padLeft(10, "-") adds "-" to the left of the originalString to make its length 10. Similarly, padRight(10, "*") adds "*" to the right to achieve the desired length.

    These are simple examples to get you started with trimming and padding in Dart. Adjust the parameters according to your specific needs.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย