Thursday, February 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

How to Split a String into Substrings in JavaScript

learningcode_x1mckf by learningcode_x1mckf
September 14, 2022
in JavaScript
0
How to Split a String into Substrings in JavaScript
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

Pay What You Want for this Learn to Code JavaScript Certification Bundle

How to have a Smooth/Fast scroll in mobile popup window? – JavaScript – SitePoint Forums

JavaScript Token (JS) Do the Risks Outweigh the Rewards Wednesday?

On this tutorial, you’ll be taught the other ways you may break up a string into substrings, and when every methodology is beneficial.

Strings may be simply manipulated in JavaScript for various functions — utilizing native strategies obtainable within the language. We’ve just lately coated how to convert a number to a string and how to convert a string to a number in JavaScript.

One other method a string may be manipulated is by extracting components of it for use for one thing else. For instance, you may need a full URL however wish to extract simply the hash half. Otherwise you may need a listing of things separated by commas and wish to use every of this stuff individually.

Cut up a String into Substrings Utilizing substring()

All strings in JavaScript have a substring() methodology. This methodology can be utilized to retrieve a substring at particular indices.

substring() accepts two parameters. The primary one is required, and signifies the index the substring begins at. The second is non-obligatory, and signifies the index the substring ends at. If the second parameter is omitted, the substring will begin on the index offered as a primary parameter and proceed till the tip of the string.

It’s necessary to notice that the primary parameter is a 0-based index, that means the primary character is at index 0, the second is at index 1, and so forth. One other necessary factor to notice is that the second parameter is one better than the index you need the substring to finish at. For instance, if you’d like the string to finish at index 12, you present 13 for the second parameter.

For instance:

const a = 'Bytes and bits';
const b = a.substring(10, 13);
console.log(b); 
console.log(a); 

On this instance, substring() is used on the variable a to retrieve a substring. The substring begins on the index 10 and ends on the index 13. The returned worth is bit. Discover that substring() returns the substring with out mutating the worth of the variable it’s used on.

See the Pen
Using substring to split string
by SitePoint (@SitePoint)
on CodePen.

Retrieving Indices

Usually, you gained’t know the beginning or finish indices of the substring whereas writing the code. The index may very well be based mostly on different inputs or variables.

In these circumstances, you should use the indexOf() methodology. This methodology returns the index of a substring in a string if it happens in it. If the substring doesn’t exist within the string, it returns -1.

When you retrieve the index utilizing indexOf(), you may retrieve the substring.

For instance:

const url = 'https://sitepoint.com#chapter_1';
const hash = url.indexOf('#');
if (hash !== -1) 
  console.log(url.substring(hash)); 

On this instance, you retrieve the index of the hash character # within the variable url. If the worth of the index isn’t -1, you retrieve the substring from url beginning on the index of the hash.

You’ll be able to attempt it out within the following CodePen demo.

See the Pen
Using substring with indexOf to split string
by SitePoint (@SitePoint)
on CodePen.

Cut up a String into Substrings Utilizing break up()

One other helpful option to retrieve a substring from a string is the break up() methodology. In case your string is a listing of things separated by a delimiter, you should use the break up() methodology to separate the string into an array of substrings based mostly on the delimiter.

break up() accepts two non-obligatory parameters. The primary parameter is the delimiter that ought to be used to find out how the string is break up. If none is offered, an array is returned with one merchandise which is the string as a complete.

The second parameter is a quantity that limits the variety of substrings returned within the array. If offered, the string is break up on the delimiter till the restrict is reached, and the remainder of the textual content within the string shall be omitted from the array.

For instance:

const str = 'Toyota,Nissan,Mercedes,Tesla';
const automobiles = str.break up(',');
console.log(automobiles); 

On this instance, break up() is used on a string that comprises a listing of automobile model names separated by the , delimiter. The returned array comprises every automobile model title as an merchandise within the array.

Notice that break up() returns the array of substrings with out affecting the worth of the string it’s used on.

The next dwell instance demonstrates how a string separated by commas may be break up into listing gadgets.

See the Pen
Using split to get substrings
by SitePoint (@SitePoint)
on CodePen.

Conclusion

On this tutorial, you’ve discovered find out how to break up a string into substrings utilizing the strategies substring() and break up().

substring() is beneficial while you wish to retrieve a single substring from a string at a particular index. You need to use it with indexOf() to retrieve the beginning or ending index of the substring.

break up(), however, is beneficial when you could have a string that comprises a listing of things separated by a delimiter, resembling a comma. You’ll be able to then break up the string into an array of substrings utilizing break up().

If you happen to discovered this text helpful, you may additionally get pleasure from the next:



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Pay What You Want for this Learn to Code JavaScript Certification Bundle

by learningcode_x1mckf
February 2, 2023
0
Pay What You Want for this Learn to Code JavaScript Certification Bundle

Deal Neowin Offers · Oct 4, 2021 - Up to date Jan 31, 2023 13:00 EST Jumpstart your profitable profession in coding and programmingRight now's highlighted deal comes...

Read more

How to have a Smooth/Fast scroll in mobile popup window? – JavaScript – SitePoint Forums

by learningcode_x1mckf
February 2, 2023
0
Different server for Google API – JavaScript – SitePoint Forums

Hello Associates,Sorry I need to appropriate the positioning tackle to this: http://dev.harfrooz.com/I searched quite a bit and I came upon that my downside is expounded to iscroll.js File....

Read more

JavaScript Token (JS) Do the Risks Outweigh the Rewards Wednesday?

by learningcode_x1mckf
February 1, 2023
0
JavaScript Token (JS) Do the Risks Outweigh the Rewards Wednesday?

News Home Wednesday, February 01, 2023 07:38 AM | InvestorsObserver Analysts JavaScript Token receives a excessive risk score from InvestorsObserver evaluation. The proprietary scoring system analyzes how a...

Read more

Discord Rich Presence – JavaScript – SitePoint Forums

by learningcode_x1mckf
February 1, 2023
0
Different server for Google API – JavaScript – SitePoint Forums

Hiya! Extraordinarily new to java-script and I’m making an attempt to make use of discordjs-rpc to make one thing that can change my standing based mostly on no...

Read more

WebAssembly vs. JavaScript: Security, Speed, Flexibility

by learningcode_x1mckf
February 1, 2023
0
WebAssembly vs. JavaScript: Security, Speed, Flexibility

In direction of the start of what's popularly referred to as the World Extensive Net, there was JavaScript. JavaScript has been round since 1995 when Brendan Eich created...

Read more
Next Post
Junior Java Developer at Reverside – Gauteng saon_careerjunctionza_state

Junior Java Developer at Reverside - Gauteng saon_careerjunctionza_state

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

COBOL programming skills gap thwarts modernization to Java

COBOL programming skills gap thwarts modernization to Java

November 6, 2022
Police Chief Reveals East Java Police Chief Teddy Minahasa’s Alleged Drug Case

Police Chief Reveals East Java Police Chief Teddy Minahasa’s Alleged Drug Case

October 14, 2022
Stealthy Malware Written in C/C++ Steal Directories

Stealthy Malware Written in C/C++ Steal Directories

October 19, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java :Full Stack Developer – Western Cape saon_careerjunctionza_state
  • Pay What You Want for this Learn to Code JavaScript Certification Bundle
  • UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?