how to get the indexes of all the occurences of words in string?2019 Community Moderator ElectionHow does database indexing work?How to check if a string contains a substring in BashHow do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I check if a string contains a specific word?How do I convert a String to an int in Java?

Drawing the Möbius band and the Klein bottle

Paper published similar to PhD thesis

Can a Mexican citizen living in US under DACA drive to Canada?

How can friction do no work in case of pure rolling?

How spaceships determine each other's mass in space?

Naming Characters after Friends/Family

What are Radio-location Services in the 1.9-2.0 MHz range?

Is there a way to find out the age of climbing ropes?

How do you make a gun that shoots melee weapons and/or swords?

Why would the IRS ask for birth certificates or even audit a small tax return?

Why aren't there more gauls like Obelix?

Does the in-code argument passing conventions used on PDP-11's have a name?

Searching for a string that contains the file name

Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?

Remove object from array based on array of some property of that object

Dukha vs legitimate need

What is "desert glass" and what does it do to the PCs?

I've given my players a lot of magic items. Is it reasonable for me to give them harder encounters?

Named nets not connected in Eagle board design

Affine transformation of circular arc in 3D

Can a space-faring robot still function over a billion years?

Is every open circuit a capacitor?

3.5% Interest Student Loan or use all of my savings on Tuition?

Calculate total length of edges in select Voronoi diagram



how to get the indexes of all the occurences of words in string?



2019 Community Moderator ElectionHow does database indexing work?How to check if a string contains a substring in BashHow do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I check if a string contains a specific word?How do I convert a String to an int in Java?










0















I want to get indexes of all the occurences of string_to_be_search



Input:



String line="hello this is prajakta , how are you?? hello this is prajakta!"
String text_to_search= "hello this is prajakta"


Here the occurrences of text_to_search is 2 so I need list of starting indexes



Output:



List l=[0,39]


Also I have tried a code below



 public List getIndexesOfMultipleOccuredString(String originalString,String textToSearch) 
int i, last = 0, count = 0;
List l = new ArrayList();
do
i = originalString.indexOf(textToSearch, last);
if (i != -1) l.add(i);
last = i + textToSearch.length();
while (i != -1);
return l;



BUT
if my input is as follows



String line="hello this is prajakta ,i love to drive car and i am a carpainter"
String text_to_search="car"


Output:



It gives me two indexes as carpainter contains car which i don't want
Output should be [39]









share|improve this question
























  • What kind of solutions have you tried so far where the second scenario does not work as you would like?

    – SCCC
    yesterday











  • @SCCC i have added my code

    – PrajaktaParkhade
    yesterday











  • @zums: Please don't modify code posted by OP. It creates confusion and problems.

    – Rohit5k2
    yesterday












  • @Rohit5k2 I could not able modify directly the code. First it get reviewed.

    – zums
    yesterday











  • @Rohit5k2 you mean the output which posted above is correct for input??

    – zums
    yesterday















0















I want to get indexes of all the occurences of string_to_be_search



Input:



String line="hello this is prajakta , how are you?? hello this is prajakta!"
String text_to_search= "hello this is prajakta"


Here the occurrences of text_to_search is 2 so I need list of starting indexes



Output:



List l=[0,39]


Also I have tried a code below



 public List getIndexesOfMultipleOccuredString(String originalString,String textToSearch) 
int i, last = 0, count = 0;
List l = new ArrayList();
do
i = originalString.indexOf(textToSearch, last);
if (i != -1) l.add(i);
last = i + textToSearch.length();
while (i != -1);
return l;



BUT
if my input is as follows



String line="hello this is prajakta ,i love to drive car and i am a carpainter"
String text_to_search="car"


Output:



It gives me two indexes as carpainter contains car which i don't want
Output should be [39]









share|improve this question
























  • What kind of solutions have you tried so far where the second scenario does not work as you would like?

    – SCCC
    yesterday











  • @SCCC i have added my code

    – PrajaktaParkhade
    yesterday











  • @zums: Please don't modify code posted by OP. It creates confusion and problems.

    – Rohit5k2
    yesterday












  • @Rohit5k2 I could not able modify directly the code. First it get reviewed.

    – zums
    yesterday











  • @Rohit5k2 you mean the output which posted above is correct for input??

    – zums
    yesterday













0












0








0








I want to get indexes of all the occurences of string_to_be_search



Input:



String line="hello this is prajakta , how are you?? hello this is prajakta!"
String text_to_search= "hello this is prajakta"


Here the occurrences of text_to_search is 2 so I need list of starting indexes



Output:



List l=[0,39]


Also I have tried a code below



 public List getIndexesOfMultipleOccuredString(String originalString,String textToSearch) 
int i, last = 0, count = 0;
List l = new ArrayList();
do
i = originalString.indexOf(textToSearch, last);
if (i != -1) l.add(i);
last = i + textToSearch.length();
while (i != -1);
return l;



BUT
if my input is as follows



String line="hello this is prajakta ,i love to drive car and i am a carpainter"
String text_to_search="car"


Output:



It gives me two indexes as carpainter contains car which i don't want
Output should be [39]









share|improve this question
















I want to get indexes of all the occurences of string_to_be_search



Input:



String line="hello this is prajakta , how are you?? hello this is prajakta!"
String text_to_search= "hello this is prajakta"


Here the occurrences of text_to_search is 2 so I need list of starting indexes



Output:



List l=[0,39]


Also I have tried a code below



 public List getIndexesOfMultipleOccuredString(String originalString,String textToSearch) 
int i, last = 0, count = 0;
List l = new ArrayList();
do
i = originalString.indexOf(textToSearch, last);
if (i != -1) l.add(i);
last = i + textToSearch.length();
while (i != -1);
return l;



BUT
if my input is as follows



String line="hello this is prajakta ,i love to drive car and i am a carpainter"
String text_to_search="car"


Output:



It gives me two indexes as carpainter contains car which i don't want
Output should be [39]






android string indexing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







PrajaktaParkhade

















asked yesterday









PrajaktaParkhadePrajaktaParkhade

136




136












  • What kind of solutions have you tried so far where the second scenario does not work as you would like?

    – SCCC
    yesterday











  • @SCCC i have added my code

    – PrajaktaParkhade
    yesterday











  • @zums: Please don't modify code posted by OP. It creates confusion and problems.

    – Rohit5k2
    yesterday












  • @Rohit5k2 I could not able modify directly the code. First it get reviewed.

    – zums
    yesterday











  • @Rohit5k2 you mean the output which posted above is correct for input??

    – zums
    yesterday

















  • What kind of solutions have you tried so far where the second scenario does not work as you would like?

    – SCCC
    yesterday











  • @SCCC i have added my code

    – PrajaktaParkhade
    yesterday











  • @zums: Please don't modify code posted by OP. It creates confusion and problems.

    – Rohit5k2
    yesterday












  • @Rohit5k2 I could not able modify directly the code. First it get reviewed.

    – zums
    yesterday











  • @Rohit5k2 you mean the output which posted above is correct for input??

    – zums
    yesterday
















What kind of solutions have you tried so far where the second scenario does not work as you would like?

– SCCC
yesterday





What kind of solutions have you tried so far where the second scenario does not work as you would like?

– SCCC
yesterday













@SCCC i have added my code

– PrajaktaParkhade
yesterday





@SCCC i have added my code

– PrajaktaParkhade
yesterday













@zums: Please don't modify code posted by OP. It creates confusion and problems.

– Rohit5k2
yesterday






@zums: Please don't modify code posted by OP. It creates confusion and problems.

– Rohit5k2
yesterday














@Rohit5k2 I could not able modify directly the code. First it get reviewed.

– zums
yesterday





@Rohit5k2 I could not able modify directly the code. First it get reviewed.

– zums
yesterday













@Rohit5k2 you mean the output which posted above is correct for input??

– zums
yesterday





@Rohit5k2 you mean the output which posted above is correct for input??

– zums
yesterday












1 Answer
1






active

oldest

votes


















0














This is how you do it using regex(word matching)



String line= "hello this is prajakta , how are you?? hello this is prajakta!";
String text_to_search = "\bhello this is prajakta\b";
ArrayList<Integer> list = new ArrayList<>();
Pattern p = Pattern.compile(text_to_search);
Matcher m = p.matcher(line);

while (m.find())
list.add(m.start());


Log.i("All occurrences", "values are " + list.toString());


Output: [0, 39]



If you search using these strings



String line="hello this is prajakta ,i love to drive car and i am a carpainter";
String text_to_search="car";// use as "\bcar\b"


Output [40]






share|improve this answer

























  • Thank you so much !!!!

    – PrajaktaParkhade
    yesterday










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55019999%2fhow-to-get-the-indexes-of-all-the-occurences-of-words-in-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This is how you do it using regex(word matching)



String line= "hello this is prajakta , how are you?? hello this is prajakta!";
String text_to_search = "\bhello this is prajakta\b";
ArrayList<Integer> list = new ArrayList<>();
Pattern p = Pattern.compile(text_to_search);
Matcher m = p.matcher(line);

while (m.find())
list.add(m.start());


Log.i("All occurrences", "values are " + list.toString());


Output: [0, 39]



If you search using these strings



String line="hello this is prajakta ,i love to drive car and i am a carpainter";
String text_to_search="car";// use as "\bcar\b"


Output [40]






share|improve this answer

























  • Thank you so much !!!!

    – PrajaktaParkhade
    yesterday















0














This is how you do it using regex(word matching)



String line= "hello this is prajakta , how are you?? hello this is prajakta!";
String text_to_search = "\bhello this is prajakta\b";
ArrayList<Integer> list = new ArrayList<>();
Pattern p = Pattern.compile(text_to_search);
Matcher m = p.matcher(line);

while (m.find())
list.add(m.start());


Log.i("All occurrences", "values are " + list.toString());


Output: [0, 39]



If you search using these strings



String line="hello this is prajakta ,i love to drive car and i am a carpainter";
String text_to_search="car";// use as "\bcar\b"


Output [40]






share|improve this answer

























  • Thank you so much !!!!

    – PrajaktaParkhade
    yesterday













0












0








0







This is how you do it using regex(word matching)



String line= "hello this is prajakta , how are you?? hello this is prajakta!";
String text_to_search = "\bhello this is prajakta\b";
ArrayList<Integer> list = new ArrayList<>();
Pattern p = Pattern.compile(text_to_search);
Matcher m = p.matcher(line);

while (m.find())
list.add(m.start());


Log.i("All occurrences", "values are " + list.toString());


Output: [0, 39]



If you search using these strings



String line="hello this is prajakta ,i love to drive car and i am a carpainter";
String text_to_search="car";// use as "\bcar\b"


Output [40]






share|improve this answer















This is how you do it using regex(word matching)



String line= "hello this is prajakta , how are you?? hello this is prajakta!";
String text_to_search = "\bhello this is prajakta\b";
ArrayList<Integer> list = new ArrayList<>();
Pattern p = Pattern.compile(text_to_search);
Matcher m = p.matcher(line);

while (m.find())
list.add(m.start());


Log.i("All occurrences", "values are " + list.toString());


Output: [0, 39]



If you search using these strings



String line="hello this is prajakta ,i love to drive car and i am a carpainter";
String text_to_search="car";// use as "\bcar\b"


Output [40]







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday









zums

959619




959619










answered yesterday









Rohit5k2Rohit5k2

14.6k42651




14.6k42651












  • Thank you so much !!!!

    – PrajaktaParkhade
    yesterday

















  • Thank you so much !!!!

    – PrajaktaParkhade
    yesterday
















Thank you so much !!!!

– PrajaktaParkhade
yesterday





Thank you so much !!!!

– PrajaktaParkhade
yesterday



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55019999%2fhow-to-get-the-indexes-of-all-the-occurences-of-words-in-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

Алба-Юлія

Захаров Федір Захарович