从参考字符串替换部分字符串

时间:2014-09-25 17:52:11

标签: java string replace

我有一个String ArrayList,其中包含一个字母,后跟一个数字作为每个字母的后缀。

ArrayList <String> baseOctave = new ArrayList();
baseOctave.add("S1");
baseOctave.add("R2");
baseOctave.add("G4");
baseOctave.add("M2");
baseOctave.add("P3");
baseOctave.add("D1");
baseOctave.add("N1");

我从这个baseOctave和其他几个字符传递字符串作为创建对象的输入模式。     MyClass obj1 = new MyClass(&#34; S1 ,, R2。,M2&#39;&#39; - &#34;); 由于我经常在对象实例化过程中使用这些输入模式,所以我想使用简单的字符S,R,G,M等。 例如:

   MyClass obj1 = new MyClass ("S,,R.,M''-");
    MyClass obj2 = new MyClass ("S1,G.,M,D1");

因此,在对象创建过程中使用的字母可能包含数字作为后缀,或者可能没有数字作为后缀。

但是在构造函数(或单独的方法)中,我想用带后缀的字母替换这些简单的字母。后缀取自baseOctave。 例如:obj1和obj2中的两个字符串应该是&#34; S1,,R2。,M2&#39;&#39; - &#34;和&#34; S1,G4。,M2,D1&#34;

我打算这样做,但无法继续下面的代码。需要一些帮助才能取代..

 static void addSwaraSuffix(ArrayList<String> pattern) {

        for (int index = 0; index < pattern.size(); index++) {
            // Get the patterns one by one from the arrayList and verify and manipulate if necessary.
            String str = pattern.get(index);

            // First see if the second character in Array List element is digit or not.
            // If digit, nothing should be done. 
            //If not, replace/insert the corresponding index from master list
            if (Character.isDigit(str.charAt(1)) != true) {

                // Replace from baseOctave.

                str = str.replace(str.charAt(0), ?);    // replace with appropriate alphabet having suffix from baseOctave.

                // Finally put the str back to arrayList.
                pattern.set(index, str);
            }
        }
    }

编辑信息如下: 谢谢你的回答。我找到了另一个解决方案并且运行正下面是我发现的完整代码。如果有任何问题,请告诉我。

static void addSwaraSuffix(ArrayList<String> inputPattern, ArrayList<String> baseOctave) {
        String temp = "";
        String str;
        for (int index = 0; index < inputPattern.size(); index++) {
            str = inputPattern.get(index);
            // First see if the second character in Array List is digit or not.
            // If digit, nothing should be done. If not, replace/insert the corresponding index from master list
            // Sometimes only one swara might be there. Ex: S,R,G,M,P,D,N 
            if (((str.length() == 1)) || (Character.isDigit(str.charAt(1)) != true)) {
         // Append with index.
                // first find the corresponsing element to be replaced from baseOctave.
                for (int index2 = 0; index2 < baseOctave.size(); index2++) {
                    if (baseOctave.get(index2).startsWith(Character.toString(str.charAt(0)))) {
                        temp = baseOctave.get(index2);
                        break;
                    }

                }
                str = str.replace(Character.toString(str.charAt(0)), temp);

            }
            inputPattern.set(index, str);
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为缩写只是一个字符,而在完整模式中,第二个字符总是数字。以下代码依赖于此假设,因此如果错误请通知我。

        static String replace(String string, Collection<String> patterns) {
            Map<Character, String> replacements = new HashMap<Character, String>(patterns.size());
            for (String pattern : patterns) {
                replacements.put(pattern.charAt(0), pattern);
            }

            StringBuilder result = new StringBuilder();

            for (int i = 0; i < string.length(); i++) {
                Character c = string.charAt(i);
                char next = i < string.length() - 1 ? string.charAt(i + 1) : ' ';

                String replacement = replacements.get(c);
                if (replacement != null && (next <= '0' || next >= '9')) {
                    result.append(replacement);
                } else {
                    result.append(c);
                }
            }

            return result.toString();
        }


        public static void main(String[] args) {
            ArrayList<String> baseOctave = new ArrayList<String>();
            baseOctave.add("S1");
            baseOctave.add("R2");
            baseOctave.add("G4");
            baseOctave.add("M2");
            baseOctave.add("P3");
            baseOctave.add("D1");
            baseOctave.add("N1");

            System.out.println(replace("S,,R.,M''-", baseOctave));
            System.out.println(replace("S1,G.,M,D1", baseOctave));
            System.out.println(replace("", baseOctave));
            System.out.println(replace("S", baseOctave));
        }

结果:

 S1,,R2.,M2''-
 S1,G4.,M2,D1

 S1