Text Tools
String Utilities

String Utilities

Enter the separator to split the input string


What is a string?

In computer programming, a string is a data type in the text form. A string is comprised of a sequence of characters, such as letters, numbers, and symbols. Generally, a string in programming is enclosed in either double quotes "", single quotes '', or backticks ``.

For example:

  • "computer" - A string representing the word computer. This is a normal string consisting of a set of Latin alphabets to form the word computer.

  • "123" - A string representing the number 123. Look closely that it's enclosed in the quotation marks. Therefore, "123" is treated as a string which is a complete different data type from the integer 123 in computer programming.

  • "!@#&%" - A string consisting of a sequence of random symbols. A string can be anything in the text form literally including any symbols.

  • "" - An empty string with zero length. An empty string is still considered a string as it represents a sequence of zero characters.

What is a substring?

A substring is a set of contiguous characters within a string. For example, app is a substring of the word application whereas apple is not. A substring can be a prefix or suffix as well.

In most programming languages, there exists the substring method used to extract a subset of characters from a string given the start and end or length as parameters.

The following is an example of how to extract a substring from a string using the substring method in JavaScript.

const str = 'JavaScript rocks!';

console.log(str.substring(0, 10)); // JavaScript

What is the length of a string?

The length of a string is the total number of characters within the string. Most programming languages already provide the built-in length method for developers to count the length of a string.

The length method can have similar names in various programming languages, but basically, it should behave the same which always returns an integer indicating the string length.

This is an example of how to count the length of a string in JavaScript.

const str = 'Ada Lovelace was the first computer programmer.';

console.log(str.length); // 47

How to count the number of words in a string

Counting the number of words in a string can be different for each language depending on the characters in the language. To keep it simple, we're talking about counting English words so that you have an idea of how to count words in an English article programmatically.

The example below demonstrates how to count the number of words in a string in JavaScript.

function countWords(str) {
  // Split the input string by a white space " ",
  // and return the length of it.
  return str.split(' ').length;

console.log(countWords('The indexing of an array start with 0.')); // 8

How to count the number of lines in a string

Counting the number of lines in a string is very simple because you have to count only line breaks as they indicate how many lines are there in a string. However, what you have to be aware of is that a string can be comprised of different line break characters namely line feed LF and carriage return CR which have the different escape sequences \n and \r respectively.

Different OSes, such as Windows, Linux, and macOS use different line break characters, so you have to use regular expression to split them apart from normal characters in order to count line numbers properly.

This example shows how to count the number of lines in a string in JavaScript.

function countLines(str) {
  // Split the input string by a line break in both Windows and UNIX,
  // and return the length of it.
  return str.split(/\r\n|\r|\n/).length;

const str = `
  Everyone in this country should learn to program a computer, because it teaches you to think.
  The programmer's of tomorrow are the wizards of the future.

console.log(countLines(str)); // 4

How to reverse a string

String reversal may sound useless for general usage in daily life situations. However, sometimes it can be useful when you want to check if a number or a string is a palindrome especially after removing all the punctuations and white spaces or test a regular expression in some cases.

There is no built-in reverse method for strings in JavaScript, but you can easily reverse a string in JavaScript using this function.

function reverse(str) {
  // Split the input string into individual characters in the array form.
  let output = str.split('');
  // Reverse the sequence of the array using the built-in reverse method.
  output = output.reverse();
  // Join the reversed array back into a string.
  output = output.join('');

  return output;

console.log(reverse('abc')); // cba

Or even shorter by chaining all the methods together.

function reverse(str) {
  // Combine everything into one line by chaining all the methods together.
  return str.split('').reverse().join('');

console.log(reverse('abc')); // cba

Related Tools

Case Converter

Converts words or text to any case instantly, such as lowercase, uppercase, camel case, capital case, constant case, param case, pascal case, sentence case, title case, and more.

Sort Lines

Sorts lines alphanumerically and/or case-insensitively, reverses lines, shuffles lines, or adds line numbers to text with your preferred EOL for both UNIX and Windows.

Diff Checker

Compares text to find the differences between two text documents instantly with syntax highlighting. Supports over 170 programming languages.

Text Editor

Views and edits text or code with syntax highlighting and saves it into a file. Supports over 170 programming languages.

JSON Editor

Views, edits, and formats JSON data instantly with syntax highlighting and saves it into a file including JSON Viewer for in-depth JSON data inspection.

Lorem Ipsum Generator

Generates Lorem Ipsum as known as placeholder text in paragraphs, sentences, or words instantly. Supports both plain text and HTML.

URL Parser / Query String Splitter

Instantly parses a URL and splits a query string into individual components, such as protocol, path, host, port, username, password, and more.

Slug Generator

Instantly slugifies words or text to an SEO-friendly and human-readable URL slug for better SEO optimization.

HTML Stripper

Completely strips all the HTML tags from HTML code. Only text content inside the stripped HTML tags will remain. Supports optional allowed tags.


Pastes text or code for online public viewing via a share link with syntax highlighting and an optional expiration period. Supports over 170 programming languages.