/    Sign up×
Bounties /Pin to ProfileBookmark

How to split long string into two parts with JavaScript

+ 1,000
Copy linkTweet thisAlerts:
Feb 03.2023

I am working on an app that has a lot of legacy content with super long product descriptions that have keywords stuffed into them. The product catalog is about 20 million, so I’m not going to be able to easily edit all of them before going live, and it will be an iterative process. What I am trying to do is split the description strings into two parts, using the first instance of a period “.” as the break point. How can I go from this:

let description = ‘Online Education Concept. Learning Platform For University Students. Person Is Engaged In Self-Education. Distance E-Learning, Modern Technologies And Programs For Study Via Internet, Educational App’;

To this:

description[0] = ‘Online Education Concept.’;
description[1] = ‘Learning Platform For University Students. Person Is Engaged In Self-Education. Distance E-Learning, Modern Technologies And Programs For Study Via Internet, Educational App’;

to post a answer
JavaScript

5 Replies

Copy linkTweet thisAlerts:
@joesbdevFeb 06.2023 — The split() method splits a string into an array of substrings based on a specified separator string. In this case, the separator string is '.', and the maximum number of splits is set to 2 using the second argument to split().

The resulting array is then combined back into two separate strings, with descriptionPart1 being the first part of the description and descriptionPart2 being the rest.


let description = 'Online Education Concept. Learning Platform For University Students. Person Is Engaged In Self-Education. Distance E-Learning, Modern Technologies And Programs For Study Via Internet, Educational App';

let descriptionArray = description.split('.', 2);

descriptionArray[0] += '.';

let descriptionPart1 = descriptionArray[0];
let descriptionPart2 = descriptionArray.slice(1).join('.');
Copy linkTweet thisAlerts:
@ethankuhrtsFeb 05.2023 — You can try something like this


let long_string = "this is a very long string. it has multiple sentences. this is the third sentence. aaand one more for good measure."

let split_string = long_string.split(".");
let first = split_string[0];
let second = split.slice(1, -1).join();


first splits the string into an array of each sentence then you can access a specific sentence using split_string[index]

to get the rest of the sentences you use .slice(index1, index2) which collects everything between those two indexes. and finally .join after the slice to combine it back into a string.
Copy linkTweet thisAlerts:
@jimmckercharsbFeb 04.2023 — You could do something like this...


let description = ‘Online Education Concept. Learning Platform For University Students. Person Is Engaged In Self-Education. Distance E-Learning, Modern Technologies And Programs For Study Via Internet, Educational App’;
let parts = description.split('.')
let firstSentence = parts.shift().concat('.')
let theRest = parts.join('. ')
Copy linkTweet thisAlerts:
@bkmacdaddyFeb 03.2023 — You can use the JavaScript .split() method and specify the "." character as the separator.

let description = 'Online Education Concept. Learning Platform For University Students. Person Is Engaged In Self-Education. Distance E-Learning, Modern Technologies And Programs For Study Via Internet, Educational App';

let descriptionArray = description.split('.', 2);

let descriptionPart1 = descriptionArray[0] + '.';
let descriptionPart2 = descriptionArray.slice(1).join('.');

console.log(descriptionPart1); // 'Online Education Concept.'
console.log(descriptionPart2); // ' Learning Platform For University Students. Person Is Engaged In Self-Education. Distance E-Learning, Modern Technologies And Programs For Study Via Internet, Educational App'
@toddauthorThank you - this got me a lot closer... here is what I ended up doing: const firstPeriodIndex = product.name.indexOf('.') if(firstPeriodIndex > 10) { const namePart1 = product.name.substring(0, firstPeriodIndex + 1).replace(/\.$/, "") const namePart2 = product.name.substring(firstPeriodIndex + 1) product.name = namePart1 product.nameExtra = namePart2 }Feb 03.2023
Davinci•003 auto-replyvar str = "This is a long string";
var firstPart = str.substring(0, str.length/2);
var secondPart = str.substring(str.length/2);

Was this helpful?

×

Success!

Help @todd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...