0

When # comes in string, I want to split it on new line using JavaScript.

Please help me.

Sample input:

This application helps the user to instantiate #Removed#Basic#afdaf#Clip#Python#matching of many parts#

Expected output:

This helps the user to instantiate 
Removed
Basic
afdaf
Clip
Python
matching of many parts
1
  • have you tried for this by your own? then please share your code Commented Feb 6, 2019 at 5:45

4 Answers 4

1

you can simply replace '#' by '\n'

var mainVar = 'This application helps the user to instantiate#Removed#Basic#afdaf#Clip#Python#matching';
console.log(mainVar.replace(/[^\w\s]/gi, '\n'));

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

2 Comments

Thanks for your help. I used the logic which you have shared me and my code is working.
I have posted another question can you help me. stackoverflow.com/questions/54587578/…
1

Convert a string into array and loop through the array and print values one by one.

var str = "helps the user to instantiate #Removed#Basic#afdaf#Clip#Python#matching of many parts#";

    str.split("#").forEach(function(entry) {
        console.log(entry);
    });

2 Comments

I have dynamic output , how I can do it. every time str value change but it contains #. whatever code you have written is perfect but if I want to iterate then how I can do it.
Do you mean that you are getting this str value from an array?
0

You can try this:

You should use the string replace function, with a single regex. Assuming by special characters

var str = "This application helps the user to instantiate #Removed#Basic#afdaf#Clip#Python#matching of many parts#";
console.log(str.replace(/[^a-zA-Z ]/g, "\n"));

3 Comments

This should not work if there is a number or any other special character other than '#' in the given string.
Hi Rohit, Thanks for help . str value change every time so I have to use for loop to iterate over list . How I can do it.
I have to implement your code in below function. could you help me.
0

The below solution will split based on the # and store it in an array. This solution will come in handy with splitting strings.

var sentence = '#Removed#Basic#afdaf#Clip#Python#matching of many parts#'

var newSentence = [];
for(var char of sentence.split("#")){
    console.log(char); // This will print each string on a new line
    newSentence.push(char);
}
console.log(newSentence.join(" "));

Comments

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.