Java String Tokens

Sort by

recency

|

1733 Discussions

|

  • + 0 comments

    import java.util.*;

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
    

    String s = scan.nextLine(); // s = s.trim();

    // Split by commas, question marks, spaces, and apostrophes String[] s1 = s.split("[^a-zA-Z0-9]+");

    int nonEmptyCount = 0; for (String word : s1) { if (!word.isEmpty()) { nonEmptyCount++; } }

    // Print the word count first System.out.println(nonEmptyCount);

    // Print each non-empty word for (String word : s1) { if (!word.isEmpty()) { System.out.println(word); } }

    This Worked for me. scan.close();

    } }

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        // Write your code here.
    
    
        String[] s1=s.split("[^a-zA-Z]+");
        System.out.println(s1.length);
    
        for(String x:s1){
            if (!x.isEmpty()) { // Avoid empty strings
                System.out.println(x);
            }
        }
        scan.close();
    }
    

    }

  • + 0 comments

    public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        StringTokenizer st = new StringTokenizer(s, " !,?._'@]+");
        int n = st.countTokens();
        System.out.println(n);
        while(st.hasMoreTokens()){
            System.out.println(st.nextToken());
        }
    }
    

    }

  • + 0 comments

    import java.io.; import java.util.; /* *@Author : Rehan *Using Stream API in Java8 */ public class SolutionDump {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        scan.close();
        if (s.trim().length()==0 || s.trim().length()>400000){
        System.out.println(0);
        return;
        }
        s=s.trim();
        // Write your code here.
        List<String> count = Stream
        .of(s.split("[^(a-zA-Z)]+")).collect(Collectors.toList());
    
        System.out.println(count.size());
                //to print the expected list
        \Stream.of(s.split("[^(a-zA-Z)]+")).forEach(System.out::println);
    
    }
    

    }

  • + 0 comments

    Why it needs to trim()?

    If it doesn't trim() , it will not pass all the test even if they output the same thing

    Why?

    I would like to know

    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String s = scan.nextLine();
            scan.close();
            if (s.trim().length()==0 || s.trim().length()>400000){
            System.out.println(0);
            return;
            }
            s=s.trim();
            String regex = "[^(a-zA-Z)]+";
            String [] lista =s.split(regex);
            
           
            System.out.println(lista.length);
            
            
            for(String k:lista){
               System.out.println(k);
            }
            
            
            
        }
    }