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.
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
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
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 index of an array starts at 0.')); // 8
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
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