我有一个字符串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(“S1,R2,M2” - “); 由于我经常使对象实例化期间使用这些种输入模式,我想用简单的字符S,R,G,M等 例:
MyClass obj1 = new MyClass ("S,,R.,M''-");
MyClass obj2 = new MyClass ("S1,G.,M,D1");
所以对象创建过程中使用的字母表可以包含数字作为后缀或它可能没有数字作为后缀。
但是在构造函数中(或在单独的方法中),我想用带有后缀的字母替换这些简单的字母。后缀取自baseOctave。 例:以上OBJ1和obj2的两个字符串应该是 “S1,R2,M2 '' - ” 和 “S1,G4,M2,D1”
我绑要做到这一点,但不能继续代码如下。需要一些帮助,更换,请..
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);
}
}
您的图案元素将永远是一个字符用一个数字?缩写总是一个字符? – Aivean 2014-09-25 18:00:11
正确。我有很多baseOctave的实例,它总是有以下组合:字母S,R,G,M,P,D,N,后面跟着1-4个数字。例如:S1 R2 S4 G1 G4 M3 P1然而,我的模式可能省略这些后缀以便于处理。所以我会通过基础八度,然后模式,以便根据基础八度,模式将被修改,以包括这些后缀。 – 2014-09-26 03:33:06