get IE Version from user agent - Java Network

Java examples for Network:User Agent

Description

get IE Version from user agent

Demo Code


//package com.java2s;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) throws Exception {
        String userAgent = "java2s.com";
        System.out.println(getIeVersion(userAgent));
    }// w  ww. j  a  v a  2 s  . com

    public static int getIeVersion(String userAgent) {
        int version = 7;
        Pattern p = Pattern.compile("MSIE");
        Matcher matcher = p.matcher(userAgent);
        if (matcher.find()) {
            int start = matcher.start() + 5;
            int end = matcher.end() + 4;
            String content = userAgent.substring(start, end);
            version = (int) Double.parseDouble(content);
        }
        return version;
    }
}

Related Tutorials