0

I have String set which is "5X3I3M3X"

I want to separate 5X = 1X1X1X1X1X , depends on number in front of character "X"

So finally it could be 1X1X1X1X1X3I3M1X1X1X

I did it like below,

    String str = "5X3I3M3X";
    StringBuffer sBuffer = new StringBuffer("");

    for(int i = 1 ; i < str.length() ; i=i+2) {
        if( str.charAt(i) == 'X'){

            if(str.charAt(i-1) == 1){
                sBuffer.append(str.charAt(i-1)+""+str.charAt(i));
            }else{
                StringBuffer sBufferTemp = new StringBuffer("");

                for(int j=0 ; j < Character.getNumericValue(str.charAt(i-1)) ; j++){
                    sBufferTemp.append("1X");
                }

                sBuffer.append(sBufferTemp.toString());
            }
        } else {
            sBuffer.append(str.charAt(i-1)+""+str.charAt(i));
        }
    }
    System.out.println(sBuffer.toString());

And I want to know is there any simple way to separate that logic? note. If I want to add to separate 'I', then it should be dynamically change also like "1X1X1X1X1X1I1I1I3M1X1X1X"

I can change it my code extpend if (str.charAt(I) == 'I') ....

I hope there is another simple way to implement this.

3 Answers 3

1

Following your coding solution, I suggest the following changes:

String str = "5X3I3M3X";
StringBuilder sBuffer = new StringBuilder();
for (int i = 0; i < str.length(); ++i) {
    if (i + 1 < str.length()
            && str.charAt(i + 1) == 'X'
            && str.charAt(i) != '1'
            && Character.isDigit(str.charAt(i))) {
        int n = Character.getNumericValue(str.charAt(i));
        for (int j = 0 ; j < n; ++j) {
            sBufferTemp.append("1X");
        }
        ++i;
    } else {
        sBuffer.append(str.charAt(i));
    }
}
  • Use the newer and faster StringBuilder instead of the StringBuffer
  • Loop through all i, so the appending in the default case is straight-forward
  • The condition can be more complete, the check on '1' is not needed.
  • The char '1' must be compared against, not 1.
Sign up to request clarification or add additional context in comments.

Comments

1

You can pass a configurable character array into a method. With this configuration, you can check if the current character is expandable.

Output from program below:

1X1X1X1X1X1I1I1I1M1M1M1X1X1X
public class StringExpander {
    public static void main(String[] args) {
        System.out.println(expand("5X3I3M3X", new char[] { 'X', 'I', 'M' }));
    }

    public static String expand(String str, char[] chArr) {
        StringBuffer buff = new StringBuffer();
        for (int i = 1; i < str.length(); i = i + 2) {
            char ch = str.charAt(i);
            if (contains(chArr, ch)) {
                if (str.charAt(i - 1) == 1) {
                    buff.append(str.charAt(i - 1)).append(str.charAt(i));
                } else {
                    StringBuffer tempBuff = new StringBuffer();
                    int repeat = Character.getNumericValue(str.charAt(i - 1));
                    for (int j = 0; j < repeat; j++) {
                        tempBuff.append(1).append(ch);
                    }
                    buff.append(tempBuff);
                }
            } else {
                buff.append(str.charAt(i - 1)).append(ch);
            }
        }
        return buff.toString();
    }

    private static boolean contains(char[] arr, char val) {
        for (char ch : arr) {
            if (ch == val) {
                return true;
            }
        }
        return false;
    }
}

Comments

0

I know this isn't exactly syntactic sugar whatsoever ..

String str = "5X3I3M3X";
char[] filter = new char[]{'X', 'I', 'M'};
StringBuffer stringBuffer = new StringBuffer("");

for (int i = 0; i < str.length(); i++) {
    if (i != 0 && new String(filter).contains(Character.toString(str.charAt(i))) && Character.isDigit(str.charAt(i-1))) {
        for (int j = 0; j < Character.getNumericValue(str.charAt(i-1)); j++) {
            stringBuffer.append("1" + str.charAt(i));
        }
    } else if (i < str.length()-1 && new String(filter).contains(Character.toString(str.charAt(i+1)))) {
        //do nothing
    }
    else {
        stringBuffer.append(str.charAt(i));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.