
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Add line break inside a string conditionally in JavaScript
We are required to write a function breakString() that takes in two arguments first the string to be broken and second is a number that represents the threshold count of characters after reaching which we have to repeatedly add line breaks in place of spaces.
So, let’s do it. We will iterate over the with a for loop, we will keep a count that how many
characters have elapsed with inserting a ‘
’ if the count exceeds the limit and we encounter a
space we replace it with line break in the new string and reset the count to 0 otherwise we keep
inserting the original string characters in the new string and keep increasing the count.
The full code for the same will be −
const text = 'Hey can I call you by your name?'; const breakString = (str, limit) => { let brokenString = ''; for(let i = 0, count = 0; i < str.length; i++){ if(count >= limit && str[i] === ' '){ count = 0; brokenString += '
'; }else{ count++; brokenString += str[i]; } } return brokenString; } console.log(breakString(text, 4));
Following is the console output −
Hey can I call you by your name?
- Related Articles
- Add line break inside string only between specific position and only if there is a white space JavaScript
- How to conditionally add a member to an object using JavaScript?
- How to add a line break in an android textView?
- How to create a line break with JavaScript?
- How to add a line break in an Android TextView using Kotlin?
- How to add line break for UILabel in iOS/iPhone?
- How to use a line break in array values in JavaScript?
- Conditionally change object property with JavaScript?
- Usage of page-break-before, page-break-after, and page-break-inside properties in CSS
- How to set the page-break behavior inside an element with JavaScript?
- How to divide a string by line break or period with Python regular expressions?
- How to Add a String After Each Line in a File in Linux?
- How to insert a single line break in HTML?
- Check If a String Can Break Another String in C++
- How to add a number and a string in JavaScript?

Advertisements