Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all ,

I have an array read as a string[]
Now is a huge text with each new words separated with a comma
I want to read and save data, but when a certain word appears I want to stop and just save only save data uptill that point.

here is my code where are my going wrong?
for now everything still gets saved .

JavaScript
var mylist=Somerawdata;
var expectedWords="my name is Garner Eric";
for(let i =0;i<.length;i++){
if(mylist[i]===expectedWords)break
console.log(mylist[i]);
}


What I have tried:

var mylist=Somerawdata;
var expectedWords="my name is Garner Eric";
for(let i =0;i<.length;i++){
if(mylist[i]===expectedWords)break
console.log(mylist[i]);
}


For anyone looking at this after following suggestions from
W∴ Balboos, GHB

I implemented it like this ==>
This solution works as per guidance from
W∴ Balboos, GHB



JavaScript
<pre>var mylist=Somerawdata;
var expectedWords="my name is Garner Eric";
for(let i =0;i<.length;i++){
if(mylist===expectedWords)

mylist.substring(0, mylist.indexOf(expectedWords));
console.log(mylist);
Posted
Updated 27-Feb-21 5:47am
v3

You may wish to consider the javascript indexOf() method.

It returns the index value of the match ( >= 0) or a -1 if not found.

So, you can the handle the string by saving it's contents up to the found index or the entire string (if -1). Use the substring() method.

I'll leave the actual coding to you as it will reinforce the methods in your memory.


 
Share this answer
 
Comments
ekonapikin1990 26-Feb-21 10:54am    
can you please show me how, above is my code please
W Balboos, GHB 26-Feb-21 10:58am    
I do not write code for you - I tell you what needs to be done and possibly things to consider. You will learn nothing if I do it for you.

Telling you the methods to use has given you the answer. Computer programming requires effort - at least if you really want to do computer programming.

Think about why I picked out those two methods - how will they help? Solving problems takes practice - not google.
ekonapikin1990 27-Feb-21 10:47am    
Sir I have implemented the method you suggested below and it worked as expected
Thnk you for the directions

var mylist=Somerawdata;
var expectedWords="my name is Garner Eric";
for(let i =0;i<.length;i++){
if(mylist===expectedWords)

mylist.substring(0, mylist.indexOf(expectedWords));
console.log(mylist);
}
ekonapikin1990 26-Feb-21 11:03am    
thank you sir
You need to do something to split the input data at the word break positions. As it stands Javascript is treating your input as an array of single characters, so it will never find your match string.

You may find that Regular expressions - JavaScript | MDN[^] would be a better solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900