strip

Dart: How to remove leading spaces in a multiline string (using splitMapJoin)

As far as I know, there’s no built-in way to remove spaces from a multiline string in Dart, but you can write your own function to do this. For example, I just dug into the splitMapJoin method of the Dart String class, and wrote a stripMargin function like this:

String stripMargin(String s) {
    return s.splitMapJoin(
        RegExp(r'^', multiLine: true),
        onMatch: (_) => '\n',
        onNonMatch: (n) => n.trim(),
    );
}

Java - strip unwanted characters from a string

Here's a quick line of Java code that takes a given input string, strips all the characters from that string other than lowercase and uppercase letters, and returns whatever is left:

PHP: How to strip unwanted characters from a string

Table of Contents1 - Solution2 - Strip all characters but letters and numbers from a PHP string3 - Strip all characters but letters, numbers, and whitespace4 - Bonus: A complete PHP string-cleaning function

PHP string FAQ: How can I strip unwanted characters from a PHP string? (Also asked as, How can I delete unwanted characters from a PHP string?)

Back to top

Solution

Here’s a quick PHP preg_replace example that takes a given input string, and strips all the characters from the string other than letters (the lowercase letters "a-z", and the uppercase letters "A-Z"):

 

A Ruby script to remove binary (garbage) characters from a text file

Problem: You have a file that should be a plain text file, but for some reason it has a bunch of non-printable binary characters (also known as garbage characters) in it, and you'd like a Ruby script that can create a clean version of the file.

Solution: I've demonstrated how to do this in another blog post by using the Unix tr command, but in case you'd like a Ruby script to clean up a file like this, I thought I'd write up a quick program and share it here.