Java String Tokens

Sort by

recency

|

1740 Discussions

|

  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        // Write your code here.
    
    
        List<String> token = Arrays.asList(s.split("[ !,?._'@]"))
            .stream()
            .map(x -> x.trim())
            .filter(x -> !x.isEmpty())
            .collect(Collectors.toList());
    
        System.out.println(token.size());
        for (String t: token){
            System.out.println(t);
        }
    
        scan.close();
    }
    

    }

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String str=sc.nextLine();
            String delimeter = "[!\\s,\\._'@?]+";
            String [] parts = str.trim().split(delimeter);
            List<String> tokenList = new ArrayList<>();
            for (String s : parts) { 
                if (!s.isEmpty()) { 
                    tokenList.add(s); 
                    } 
                }
            System.out.println(tokenList.size());
            for (String s : parts) {
                System.out.println(s);
            }
        }
    }
    
  • + 0 comments

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

    public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); String[] tokens=s.split("[^a-zA-Z]+"); int count=0; for(String token:tokens){ if(!token.isEmpty()){ count++; } } System.out.println(count); for(String token:tokens){ if(!token.isEmpty()){ System.out.println(token); } } } }

  • + 0 comments

    public static void main(String[] args) {

                Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        int count = 0;
        String a = "[^A-Za-z]+";
        s = s.trim();
        String [] x = s.split(a);
        // System.out.println(x.length);
          if(s.isEmpty()){
            count = 0;
        }else {
            for (String s1 : x) {
                count++;
            }
        }
        System.out.println(count);
         for(String s1 : x){
            if(!s1.isEmpty()){
            System.out.println(s1);
            }
        }
        // Write your code here.
        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.
        s=s.trim();
        if(s.isEmpty()){
            System.out.println(0);
            return ;
    
        }
        String[] token=s.split("[^A-Za-z]+");
        System.out.println(token.length);
       for(String arr:token){
        System.out.println(arr);
    
       }
    
        scan.close();
    }
    

    }