With methods it looks like this:

String srt = "Hello world it is my first program "; String aa[] = srt.split(" "); for (int i = 0; i < aa.length; i++) { System.out.println(aa[i]); } System.out.println(); char che = 'o'; String tar; for (int i = 0; i < aa.length; i++) { tar = aa[i]; char[] oo = tar.toCharArray(); for (int j = 0; j < oo.length; j++) if (oo[j] == che) { String y = aa[i]; System.out.println(y); break; } } 
  • those. break string String by words, without using String methods? - Chubatiy
  • go over the line .... concatenating letters. as soon as you find the space character - send the concatenated letters to the array, null the concatenation variable and continue further - Alexey Shimansky
  • I would also refactor this code ... why do we need tar oo y variables. And if the task is without methods from the String class, for example, by the regular \\s[^\\s]*o[^\\s]*\\s - pavel
  • Alexey Shimansky ․․․ character space ?, as - Artak
  • @ArtakKostanyan Well, the space is what? not the same word - Alexey Shimansky

1 answer 1

I didn’t quite understand the piece of the code with the search for the word with the letter “o” and how it relates to your question, but if you just need to break the line into words without using Split (based on your question, despite your code), then you can use regular expressions:

 String s = "Hello world it is my first program"; Pattern pattern = Pattern.compile("\\w+"); Matcher matcher = pattern.matcher(s); while (matcher.find()) { System.out.println(matcher.group()); } 

Conclusion:

enter image description here

  • by code, I realized that I was looking for 1 word containing the letter o . - pavel
  • not one but everything, but without methods - Artak
  • @pavel yes, the 1st word is searched and the code stops, the code has nothing to do with the question, I did not understand this. - Denis
  • @ArtakKostanyan your question is how to break a string into words without using the String class methods. The answer is given above in my code - using regular expressions. - Denis