001    package org.expasy.jpl.commons.base.string;
002    
003    
004    import java.util.ArrayList;
005    import java.util.Iterator;
006    import java.util.List;
007    
008    
009    public final class StringUtils {
010            
011            /**
012             * Return the indices of a motif found within a target string.
013             * 
014             * @param target the string to search within.
015             * @param motif the string to search for.
016             * @return a list of positive integers (empty list if motif was not found).
017             */
018            public static final List<Integer> indicesOf(String target, String motif) {
019                    List<Integer> list = new ArrayList<Integer>();
020                    
021                    int index = target.indexOf(motif);
022                    
023                    while (index >= 0) {
024                            list.add(index);
025                            index = target.indexOf(motif, index + motif.length());
026                    }
027                    
028                    return list;
029            }
030            
031            /**
032             * Return the number of time a motif is found within a target string.
033             * 
034             * @param target the string to search within.
035             * @param motif the string to search for.
036             * @return the number of motifs found (0 if no motif was found).
037             */
038            public static final int numOfString(String target, String motif) {
039                    return indicesOf(target, motif).size();
040            }
041            
042            public static String join(Iterable<String> it, String delimiter) {
043                    if (it == null || !it.iterator().hasNext())
044                            return "";
045                    Iterator<String> iter = it.iterator();
046                    StringBuilder builder = new StringBuilder(iter.next());
047                    while (iter.hasNext()) {
048                            builder.append(delimiter).append(iter.next());
049                    }
050                    return builder.toString();
051            }
052            
053    }