Home Web Front-end JS Tutorial 15 JavaScript String Functions

15 JavaScript String Functions

Mar 04, 2025 am 12:57 AM

15 JavaScript String Functions

This guide provides an overview of 15 basic JavaScript string functions that are ideal for jQuery scripts and other string operations.

Core points:

  • This article introduces 15 basic JavaScript string functions, including charAt(x), charCodeAt(x), concat(v1, v2,…), fromCharCode(c1, c2,…), indexOf(substr, [start]), lastIndexOf(substr, [start]), match(regexp), replace(regexp/substr, replacetext), search(regexp), slice(start, [end]), split(delimiter, [limit]), substr(start, [length]), substring(from, [to]), toLowerCase(), toUpperCase(),
  • ,
  • ,
  • ,
  • , charAt(), charCodeAt(), trim(),
  • ,
,

, and .

Each function comes with usage examples and expected output, providing a practical guide for those who learn JavaScript or need to review these string functions. This article also answers some common questions about JavaScript string functions, such as the difference between and , how to convert a string to an array or change its case, what is for it, and how to check if a string contains a specific word. Example of JavaScript string function:
  1. charAt(x) Returns the character at the "x" position in the string.
// charAt(x)
var myString = 'jQuery FTW!!!';
console.log(myString.charAt(7));
// 输出:F
Copy after login
Copy after login
  1. charCodeAt(x) Returns the Unicode value of the character "x" position in the string.
// charCodeAt(position)
var message = "jquery4u";
// 警报显示 "q"
alert(message.charCodeAt(1));
Copy after login
Copy after login
  1. concat(v1, v2,…) Combine one or more strings (parameters v1, v2, etc.) into an existing string and return the combined string. The original string will not be modified.
// concat(v1, v2,..)
var message = "Sam";
var final = message.concat(" is a", " hopeless romantic.");
// 警报显示 "Sam is a hopeless romantic."
alert(final);
Copy after login
Copy after login
  1. fromCharCode(c1, c2,…) Create a string using the specified sequence of Unicode values ​​(parameters c1, c2, etc.). is a method of the String object, not a method of the String instance. For example: String.fromCharCode().
// fromCharCode(c1, c2,...)
console.log(String.fromCharCode(97, 98, 99, 120, 121, 122));
// 输出:abcxyz
console.log(String.fromCharCode(72, 69, 76, 76, 79));
// 输出:HELLO
Copy after login
Copy after login
  1. indexOf(substr, [start]) Search and return the index number of the character or substring searched in (if found). If not found, return -1. "Start" is an optional parameter that specifies where the search starts in the string. The default is 0.
// indexOf(char/substring)
var sentence = "Hi, my name is Sam!";
if (sentence.indexOf("Sam") != -1)
  alert("Sam is in there!");
Copy after login
  1. lastIndexOf(substr, [start]) Search and return the index number of the character or substring searched in (if found). Start the search from the end of the string. If not found, return -1. "Start" is an optional parameter that specifies where the search starts in the string. The default is string.length-1.
// lastIndexOf(substr, [start])
var myString = 'javascript rox';
console.log(myString.lastIndexOf('r'));
// 输出:11
Copy after login
  1. match(regexp) Performs a search for matches in a string based on regular expressions. If a match is found, an array containing information is returned; if no match is found, null is returned.
// match(regexp) // 只选择整数
var intRegex = /[0-9 -()+]+$/;

var myNumber = '999';
var myInt = myNumber.match(intRegex);
console.log(myInt); // 输出:999

var myString = '999 JS Coders';
var myInt = myString.match(intRegex);
console.log(myInt); // 输出:null
Copy after login
  1. replace(regexp/substr, replacetext) Search and replace the regular expression (or substring) part (match) to replace text.
// replace(substr, replacetext)
var myString = '999 JavaScript Coders';
console.log(myString.replace(/JavaScript/i, "jQuery"));
// 输出:999 jQuery Coders

// replace(regexp, replacetext)
var myString = '999 JavaScript Coders';
console.log(myString.replace(new RegExp("999", "gi"), "The"));
// 输出:The JavaScript Coders
Copy after login
  1. search(regexp) Test the match in the string. If a match is found, the index of the match is returned; if not found, -1 is returned.
// search(regexp)
var intRegex = /[0-9 -()+]+$/;

var myNumber = '999';
var isInt = myNumber.search(intRegex);
console.log(isInt); // 输出:0

var myString = '999 JS Coders';
var isInt = myString.search(intRegex);
console.log(isInt); // 输出:-1
Copy after login
  1. slice(start, [end]) Returns a substring of a string based on the "start" and "end" index parameters, but does not include the "end" index itself. "End" is optional, and if not specified, the slice includes all characters from "start" to the end of the string.
// slice(start, end)
var text = "excellent";
text.slice(0, 4); // 返回 "exce"
text.slice(2, 4); // 返回 "ce"
Copy after login
  1. split(delimiter, [limit]) Split the string into multiple strings according to the specified delimiter and returns an array containing each element. The optional "limit" is an integer that allows you to specify the maximum number of elements to return.
// split(delimiter)
var message = "Welcome to jQuery4u";
// word[0] 包含 "We"
// word[1] 包含 "lcome to jQuery4u"
var word = message.split("l");
Copy after login
  1. substr(start, [length]) Returns the character in the string starting from "start" and the specified number of characters "length"."Length" is optional and if omitted, it is assumed to be the end of the string.
// charAt(x)
var myString = 'jQuery FTW!!!';
console.log(myString.charAt(7));
// 输出:F
Copy after login
Copy after login
  1. substring(from, [to]) Returns the character between the "from" and "to" indexes in the string, but does not include "to" itself. "To" is optional, and if omitted, it is assumed to be the end of the string.
// charCodeAt(position)
var message = "jquery4u";
// 警报显示 "q"
alert(message.charCodeAt(1));
Copy after login
Copy after login
  1. toLowerCase() Returns a string whose characters are converted to lowercase.
// concat(v1, v2,..)
var message = "Sam";
var final = message.concat(" is a", " hopeless romantic.");
// 警报显示 "Sam is a hopeless romantic."
alert(final);
Copy after login
Copy after login
  1. toUpperCase() Returns a string whose characters are converted to uppercase.
// fromCharCode(c1, c2,...)
console.log(String.fromCharCode(97, 98, 99, 120, 121, 122));
// 输出:abcxyz
console.log(String.fromCharCode(72, 69, 76, 76, 79));
// 输出:HELLO
Copy after login
Copy after login

JavaScript String Function FAQ:

(The FAQ part is omitted here because it is basically the same as the previous output, avoiding duplication.)

I hope this modified version will meet your requirements more. Please note that I simplified the FAQ section due to space limitations. If necessary, you can add complete information.

The above is the detailed content of 15 JavaScript String Functions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles