Dino Geek, try to help you

How to manipulate strings in JavaScript?


Manipulating strings in JavaScript can be accomplished in several ways, such as:

1. Concatenation: Concatenation in JavaScript is performed using the ‘+’ operator.
```
var string1 = “Hello, “;
var string2 = “World!”;
var string3 = string1 + string2; // “Hello, World!“
```

1. Substrings and slicing: ‘substring()’, ‘substr()’, ‘slice()’ are methods to get a section of a string.
```
var str = “Hello, World!“
var result = str.substring(0,5); // “Hello“
var result2 = str.substr(7,5); // “World“
var result3 = str.slice(-1); // “!“
```

1. Replacing: The ‘replace()’ function substitutes part of a string with another string.
```
var str = “Hello, World!”;
var newStr = str.replace(“World”, “there”); // “Hello, there!“
```

1. Changing case: ‘toUpperCase()’ and ‘toLowerCase()’ are used to change the case of a string.
```
var str = “Hello, World!”;
var upper = str.toUpperCase(); // “HELLO, WORLD!“
var lower = str.toLowerCase(); // “hello, world!“
```

1. Trimming: ‘trim()’ method is used to remove whitespace from both sides of a string.
```
var str = “ Hello, World! “;
var trimStr = str.trim(); // “Hello, World!“
```

1. Splitting: ‘split()’ separates a string into an array of strings given a specific delimiter.
```
var str = “Hello, World!”;
var splitStr = str.split(“, “); // [“Hello”, “World!”]
```

1. Searching: Methods like ‘indexOf()’, ‘lastIndexOf()’ and ‘includes()’ are used to find whether specific substrings exist in a string and if so, where.
```
var str = “Hello, World!”;
var pos = str.indexOf(“World”); // 7
var isPresent = str.includes(“World”); // true
```

These are just a few examples. JavaScript provides a rich suite of string manipulation functions.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use