How do you find words without String class methods?



  • The methods look like:

    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;
            }
    }
    


  • I didn't really get a piece of code looking for a 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 it. Split (because of your question, despite your code), 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:

    введите сюда описание изображения


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2