The many ways to modify leading and trailing characters from strings with JavaScript

JavaScript provides a bunch of methods for adding and removing leading and trailing characters from strings. Today, we’re going to look a bunch of them.
Let’s dig in!
The String.padStart() method The String.padStart() method adds characters to the beginning of a string if it’s less than a certain length. This is particularly useful for numbers that need leading 0’s (but can do so much more than that).
The String.padStart() method accepts two arguments: the length the string should be, and what characters to add if it’s not that length.


This content originally appeared on Go Make Things and was authored by Go Make Things

JavaScript provides a bunch of methods for adding and removing leading and trailing characters from strings. Today, we’re going to look a bunch of them.

Let’s dig in!

The String.padStart() method

The String.padStart() method adds characters to the beginning of a string if it’s less than a certain length. This is particularly useful for numbers that need leading 0’s (but can do so much more than that).

The String.padStart() method accepts two arguments: the length the string should be, and what characters to add if it’s not that length. The characters to use is option, and defaults to a space ().

// Add a leading zero for hours below 10
let hour3 = '3';
let hour12 = '12';

// returns "03"
hour3.padStart(2, '0');

// returns "12"
hour12.padStart(2, '0');

The String.padEnd() method

The String.padEnd() method adds characters to the end of a string if it’s less than a certain length. This is particularly useful for numbers that need trailing 0’s.

The String.padEnd() method accepts two arguments: the length the string should be, and what characters to add if it’s not that length. The characters to use is option, and defaults to a space ().

// Add a leading zero for hours below 10
let minutes0 = '0';
let minutes12 = '12';

// returns "00"
minutes0.padEnd(2, '0');

// returns "12"
minutes12.padEnd(2, '0');

The String.trim() method

The String.trim() method removes leading and trailing whitespace from a string.

let str = '   I love Cape Cod potato chips.   ';

// Returns "I love Cape Cod potato chips."
str.trim();

String concatenation

You can use string concatenation to combine two or more strings together. Combine strings using the addition operator (+).

/**
 * Strings as variables
 */

let str1 = 'I love Cape Cod potato chips.';
let str2 = 'What about you?';
let concat = str1 + ' ' + str2;

// logs "I love Cape Cod potato chips. What about you?"
console.log(concat);


/**
 * Creating a new variable from two strings
 */

let concat2 =
	'I love Cape Cod potato chips. ' +
	'What about you?';

// logs "I love Cape Cod potato chips. What about you?"
console.log(concat2);


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2022-01-21T15:30:00+00:00) The many ways to modify leading and trailing characters from strings with JavaScript. Retrieved from https://www.scien.cx/2022/01/21/the-many-ways-to-modify-leading-and-trailing-characters-from-strings-with-javascript/

MLA
" » The many ways to modify leading and trailing characters from strings with JavaScript." Go Make Things | Sciencx - Friday January 21, 2022, https://www.scien.cx/2022/01/21/the-many-ways-to-modify-leading-and-trailing-characters-from-strings-with-javascript/
HARVARD
Go Make Things | Sciencx Friday January 21, 2022 » The many ways to modify leading and trailing characters from strings with JavaScript., viewed ,<https://www.scien.cx/2022/01/21/the-many-ways-to-modify-leading-and-trailing-characters-from-strings-with-javascript/>
VANCOUVER
Go Make Things | Sciencx - » The many ways to modify leading and trailing characters from strings with JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/21/the-many-ways-to-modify-leading-and-trailing-characters-from-strings-with-javascript/
CHICAGO
" » The many ways to modify leading and trailing characters from strings with JavaScript." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2022/01/21/the-many-ways-to-modify-leading-and-trailing-characters-from-strings-with-javascript/
IEEE
" » The many ways to modify leading and trailing characters from strings with JavaScript." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2022/01/21/the-many-ways-to-modify-leading-and-trailing-characters-from-strings-with-javascript/. [Accessed: ]
rf:citation
» The many ways to modify leading and trailing characters from strings with JavaScript | Go Make Things | Sciencx | https://www.scien.cx/2022/01/21/the-many-ways-to-modify-leading-and-trailing-characters-from-strings-with-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.