Java tutorial
//package com.java2s; public class Main { public static final String DOCTYPE_START = "<!DOCTYPE"; public static final String DOCTYPE_END = ">"; public static String findDocumentType(final String xml) { final int indexStart = xml.indexOf(DOCTYPE_START); // make sure we found the encoding attribute if (indexStart != -1) { final int indexEnd = xml.indexOf(DOCTYPE_END, indexStart + DOCTYPE_START.length()); // make sure we found the end of the attribute if (indexEnd != -1) { return xml.substring(indexStart, indexEnd + DOCTYPE_END.length()); } } return null; } }