Java tutorial
//package com.java2s; public class Main { public static final String ENCODING_START = "encoding=\""; public static String findEncoding(final String xml) { final int encodingIndexStart = xml.indexOf(ENCODING_START); final int firstLineBreak = xml.indexOf("\n"); // make sure we found the encoding attribute if (encodingIndexStart != -1) { final int encodingIndexEnd = xml.indexOf("\"", encodingIndexStart + ENCODING_START.length()); // make sure the encoding attribute was found before the first // line break if (firstLineBreak == -1 || encodingIndexStart < firstLineBreak) { // make sure we found the end of the attribute if (encodingIndexEnd != -1) { return xml.substring(encodingIndexStart + ENCODING_START.length(), encodingIndexEnd); } } } return null; } }