I need help in finding a specific string occurrence, if this string occurred for 5th time then I need to write to new file (from 1st to 5th occurrence), can someone please help me with that, below is the example and “endstring” is what the occurence should be on . .
start This is test data 1 endstring
start This is test data 2 endstring
start This is test data 3 endstring
start This is test data 4 endstring
start This is test data 5 endstring
start This is test data 6 endstring
start This is test data 7 endstring
start This is test data 8 endstring
start This is test data 9 endstring
start This is test data 10 endstring
start This is test data 11 endstring
this is what I’m expecting
file 1:
start This is test data 1 endstring
start This is test data 2 endstring
start This is test data 3 endstring
start This is test data 4 endstring
start This is test data 5 endstring
file 2:
start This is test data 6 endstring
start This is test data 7 endstring
start This is test data 8 endstring
start This is test data 9 endstring
start This is test data 10 endstring
start This is test data 11 endstring
How far have you gotten with this on your own? Do you have some code to show us? Where did you get stuck? What aspect of this are you having difficulty with?
If the problem is too difficult for you initially, I’d suggest solving a simpler version of it first, then try to improve it from there. For example, suppose the problem were simply to print whether a string occurs at least five times, true or false. Could you code that yourself? How about to simply print whether a string occurs at least once? That would be an even easier starting point.
It is unclear to me whether you are asking only about the String processing part of the problem or the file IO part as well.
For the String processing part, you could use the containsSlice method of the String class. Since the strings to be matched have different numerals, you could perhaps match on the common part only, “start This is test data”, then handle the numbers separately. You could also use regular expressions, but they may be overkill for this problem.
As for the file IO. that is a part of Scala (and most languages) where you just have to bite the bullet and learn the basics. There are tutorials out there that cover it well. Forums like this one are more appropriate for specific rather than general questions, unless you just want a referral to a good tutorial page.
I should have put in more details but, it’s a stream data sources I’m dealing with and the string processing part is where I’m getting confused not the file IO