Dealing with Whitespace

From Jonsdocswiki

Jump to: navigation, search

Contents

Overview

This page explains how to deal with whitespace in various languages. This includes removing leading and trailing whitespace and using whitespace to split a string.

What is whitespace

Whitespace is and character that represents itself, traditionally, as an area of white space. For example, the space between words is considered whitespace. Some computer languages care about whitespace meaning a source file has to be formatted perfectly. Other languages, for example PHP or C, do not care about whitespace and tend to ignore it completely.

Removing a trailing space

PHP

Like MySQL, PHP also has an rtrim function to remove trailing space.

Removing trailing spaces - PHP
$trimmed = rtrim("A text string with space at the end "); ;

With PHP, it's also possible to remove other whitespace characters from a string (e.g. tabs, newlines, carriage returns etc, see the PHP rtrim page for more details).

Removing trailing tabs - PHP

$text = "\t\tTabs at the beginning won't be affected.\t\t "; $trimmed = rtrim($text, " \t.");


See also

MySQL

Use the RTRIM() function:

Removing trailing spaces - MySQL
SELECT RTRIM('barbar ');


Would return:
barbar

See also

Personal tools