AppDevTools
AppDevTools
/
Text Tools
String Utilities

String Utilities

client
Enter the separator to split the input string

Documentation

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

Case Converter is a free online developer tool to convert a string to any case possible such as lowercase, uppercase, camel case, constant case, pascal case, and more.

Sort Lines

Sort Lines is a free online developer tool to sort lines alphanumerically, case-insensitively, reverse lines, shuffle lines, and add line numbers.

Diff Checker

Diff Checker is a free online developer tool to compare two strings to find the differences by line with syntax highlighting. Supports over 50 programming languages.

JSON Editor

JSON Editor is a free online developer tool to view, edit, and format JSON data with syntax highlighting including JSON Viewer for in-depth JSON inspection.

Lorem Ipsum Generator

Lorem Ipsum Generator is a free online developer tool to generate Lorem Ipsum, placeholder text, in paragraphs, sentences, or words. Supports both plain text and HTML.

URL Parser / Query String Splitter

URL Parser / Query String Splitter is a free online developer tool to parse a URL into individual components such as protocol, path, host, port, username, password, query string, and more.

Slug Generator

Slug Generator is a free online developer tool to generate SEO-friendly and human-readable URL slugs from any string. Supports multiple languages.

HTML Stripper

HTML Stripper is a free online developer tool to completely strip all the HTML tags from HTML code. Only text content will remain. Supports optional allowed tags.

Pastebin

Pastebin is a free online developer tool to paste text or code with syntax highlighting for viewing via a share link. Supports password protection.

Share