Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Understanding how to count the characters in a string in order to use slice() on a part of the string

I am learning JavaScript , the fundamentals for now and what I don’t get, is how to count the characters in the string in order to use the slice on the certain string and extract a certain word out of the string. For example

let text = "JavaScript", "apples", "avocado");
let newText = text.slice(?),(?);

How do I know or count the position of apples for example or JavaScript? Thank you!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Try like this:

let text = "JavaScript, apples, avocado";
let newText = text.slice(text.indexOf("JavaScript"));
let newText2 = text.slice(text.indexOf("apples"));
console.log(newText)
console.log(newText2)

about 4 years ago · Juan Pablo Isaza Report

0

split the string into an array, , and then splice in the string you do want. Finally, join the array elements into a new string.

const str = 'JavaScript, apples, avocado';

function replace(str, search, replacement) {

  // Create an array using `split`
  const arr = str.split(', ');
  
  // Find the index of the item you're looking for
  const index = arr.findIndex(el => el === search);

  // `splice` in the new replacement string
  arr.splice(index, 1, replacement);

  // Return the new string
  return arr.join(', ');
  
}

console.log(replace(str, 'apples', 'biscuit'));

about 4 years ago · Juan Pablo Isaza Report

0

Few observations/suggestions :

  • String will always wrapped with the quotes but in your post it is not looks like a string.
  • Why you want to slice to get the word from that string ? You can convert that string into an array and then get that word.

Implementation steps :

  • Split string and convert that in an array using String.split() method.
  • Now we can find for the word you want to search from an array using Array.find() method.

Working Demo :

// Input string
const text = "JavaScript, apples, avocado";
const searchText = 'apples';

// Split string and convert that in an array using String.split() method.
const splittedStringArray = text.split(',');

// Now we can find for the word from splittedStringArray using Array.find() method.
const result = splittedStringArray.find(item => item.trim() === searchText);

// Output
console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!