package jalview.util; public class StringUtils { /** * Returns a new character array, after inserting characters into the given * character array. * * @param in * the character array to insert into * @param position * the 0-based position for insertion * @param count * the number of characters to insert * @param ch * the character to insert */ public static final char[] insertCharAt(char[] in, int position, int count, char ch) { char[] tmp = new char[in.length + count]; if (position >= in.length) { System.arraycopy(in, 0, tmp, 0, in.length); position = in.length; } else { System.arraycopy(in, 0, tmp, 0, position); } int index = position; while (count > 0) { tmp[index++] = ch; count--; } if (position < in.length) { System.arraycopy(in, position, tmp, index, in.length - position); } return tmp; } /** * Delete * * @param in * @param from * @param to * @return */ public static final char[] deleteChars(char[] in, int from, int to) { if (from >= in.length) { return in; } char[] tmp; if (to >= in.length) { tmp = new char[from]; System.arraycopy(in, 0, tmp, 0, from); to = in.length; } else { tmp = new char[in.length - to + from]; System.arraycopy(in, 0, tmp, 0, from); System.arraycopy(in, to, tmp, from, in.length - to); } return tmp; } /** * Returns the last part of 'input' after the last occurrence of 'token'. For * example to extract only the filename from a full path or URL. * * @param input * @param token * a delimiter which must be in regular expression format * @return */ public static String getLastToken(String input, String token) { if (input == null) { return null; } if (token == null) { return input; } String[] st = input.split(token); return st[st.length - 1]; } }