c1e050fbe52db0b2f7f925b3d258c76b8e804285
[jalview.git] / src / jalview / util / StringUtils.java
1 package jalview.util;
2
3 public class StringUtils
4 {
5
6   /**
7    * Returns a new character array, after inserting characters into the given
8    * character array.
9    * 
10    * @param in
11    *          the character array to insert into
12    * @param position
13    *          the 0-based position for insertion
14    * @param count
15    *          the number of characters to insert
16    * @param ch
17    *          the character to insert
18    */
19   public static final char[] insertCharAt(char[] in, int position,
20           int count,
21           char ch)
22   {
23     char[] tmp = new char[in.length + count];
24   
25     if (position >= in.length)
26     {
27       System.arraycopy(in, 0, tmp, 0, in.length);
28       position = in.length;
29     }
30     else
31     {
32       System.arraycopy(in, 0, tmp, 0, position);
33     }
34   
35     int index = position;
36     while (count > 0)
37     {
38       tmp[index++] = ch;
39       count--;
40     }
41   
42     if (position < in.length)
43     {
44       System.arraycopy(in, position, tmp, index,
45               in.length - position);
46     }
47   
48     return tmp;
49   }
50
51   /**
52    * Delete
53    * 
54    * @param in
55    * @param from
56    * @param to
57    * @return
58    */
59   public static final char[] deleteChars(char[] in, int from, int to)
60   {
61     if (from >= in.length)
62     {
63       return in;
64     }
65
66     char[] tmp;
67
68     if (to >= in.length)
69     {
70       tmp = new char[from];
71       System.arraycopy(in, 0, tmp, 0, from);
72       to = in.length;
73     }
74     else
75     {
76       tmp = new char[in.length - to + from];
77       System.arraycopy(in, 0, tmp, 0, from);
78       System.arraycopy(in, to, tmp, from, in.length - to);
79     }
80     return tmp;
81   }
82 }