Get the first Entry splitted by separator - Java java.lang

Java examples for java.lang:String Split

Description

Get the first Entry splitted by separator

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String s = "java2s.com";
        char separator = 'o';
        System.out.println(firstEntry(s, separator));
    }/*from  w  ww .j av a2 s  .  c  om*/

    public static String firstEntry(String s, char separator) {
        int i = s.indexOf(separator);
        if (i < 0) {
            return s;
        }
        for (; i >= 0 && i < s.length();) {
            String sub = s.substring(0, i).trim();
            if (!sub.isEmpty()) {
                return sub;
            }
            s = s.substring(i + 1).trim();
            i = s.indexOf(separator);
            if (i < 0) {
                if (!s.isEmpty()) {
                    return s;
                }
                break;
            }
        }
        return null;
    }
}

Related Tutorials