Skip to content
the poor coder

trim text

Poor Man
Published
Reading time
1 min read

Trim Text in HTML#

If you have ever worked with text in HTML, you might have come across the need to trim some unwanted spaces or characters from the beginning or end of a string. This is where the trim() function comes in handy.

The trim() function is a built-in JavaScript function that removes whitespace from both ends of a string. However, it can only be used with JavaScript or jQuery. Here's an example of how to use it:

JavaScript
// Define the string
let str = "   Hello, World!   ";

// Trim the string
let trimmedStr = str.trim();

// Output the result
console.log(trimmedStr); // "Hello, World!"

Another way to trim text is by using CSS. This method only works for removing whitespace from the beginning and end of a text, but not for removing specific characters. Here's an example:

CSS
p {
  white-space: pre-line;
  text-align: justify;
}

The white-space: pre-line; property removes any extra whitespace from the beginning and end of a text, while preserving line breaks. The text-align: justify; property will evenly distribute the remaining text across the container element.

Overall, there are multiple ways to trim text in HTML, depending on your specific needs. The trim() function is great for removing both whitespace and specific characters from a string using JavaScript or jQuery. On the other hand, using CSS can help you remove unwanted whitespace from the beginning and end of a text.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC