Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.support.annotation.NonNull;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static final int VERIFY_SUCCESS = 0;
    private static final int VERIFY_LENGTH_ERROR = 1;
    private static final int VERIFY_TYPE_ERROR = 2;

    public static int verifyUsername(@NonNull String username) {

        int length = countLength(username);

        if (length < 4 || length > 20) {
            return VERIFY_LENGTH_ERROR;
        }

        String regex = "^[a-zA-Z0-9\u4E00-\u9FA5]+$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(username);

        if (!matcher.matches())
            return VERIFY_TYPE_ERROR;
        return VERIFY_SUCCESS;
    }

    private static int countLength(@NonNull String string) {
        string = string.replaceAll("[^\\x00-\\xff]", "**");
        return string.length();
    }
}