0

I have this string :

name="listLongueurs";

Using JavaScript, what is the fastest way to parse this into :

var list = "list";
var Longueurs= "Longueurs";
6
  • 1
    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 28, 2018 at 16:41
  • 2
    What is the criteria? Split it before the first capital letter? Commented Aug 28, 2018 at 16:41
  • 1
    @brittenb How is that relevant? There's no delimiter. Commented Aug 28, 2018 at 16:42
  • 2
    @Barmar If you read it, you'd see under Parameters that he split method treats the separator as a regular expression, allowing the OP to write their own rules on how to split. Commented Aug 28, 2018 at 16:43
  • Possible duplicate of split string in two on given index and return both parts Commented Aug 28, 2018 at 16:43

4 Answers 4

2

You can split the string by capital letter:

var name="listLongueurs";
var nameArr = name.split(/(?=[A-Z])/);
var list = nameArr[0];
var Longueurs= nameArr[1];
console.log(list);
console.log(Longueurs);

Sign up to request clarification or add additional context in comments.

Comments

1

To add on to Mamun's answer, you can make use of ES6 destructuring to do it more concisely:

let list, Longueurs;
[list, Longueurs] = name.split(/(?=[A-Z])/);

Comments

0

we use directly the method split like this :

mystring.split('') 

it will give us an array of alphabets of this string

Comments

-1

The fastest way for me is that :

const name = "listLongueurs".split('L').join(' L');

4 Comments

Your solution only works on that one specific example, which is likely just a minimal, reproducible example used to demonstrate the problem OP is trying to solve. Meaning that your solution doesn't work outside of the example, and is therefore not a solution.
Actually, this is exactly what was asked for. While I agree that it is likely that an answer related to capitalization is important, punishing a low answer count user for providing a valid answer to the question as stated seems vindictive.
@JonSG It's not meant to be vindictive. It's meant to entice the practice of providing minimal, reproducible examples (MRE). If people come here with questions and are repeatedly told to provide MRE, but are then presented with answers that only address the MRE, they're no longer incentivized to provide such simple examples. Providing an MRE is absolutely desirable, and in my opinion those providing solutions on the site should try to incorporate that into their answer.
There's no indication of what OP wants to split on. It could be a pattern or they could just have one single string in their program that they want to split and don't know how. In this case, an MRE was not provided because the example provided gives no indication of what OP actually wants to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.