0

I am trying to format a specific string. I had the unfortunate event of my laptop crashing on me the other day, taking away with it lots of valuable code. I did save most of the files, but the few that weren't saved were crucial to my android app. Well Here is my code that tries to format a string. I need all the numbers at the beginning of each line to go away. there are about 3600 of them.

//inside onCreate method
String string = "3    import android.animation.Animator; \n" +
"4    import android.animation.AnimatorListenerAdapter; \n" +
"5    import android.animation.ValueAnimator; \n" +
"6    import android.app.Activity; \n" +
"7    import android.content.Context; \n" +
"8    import android.content.DialogInterface; \n";

char[] ch = string.toCharArray();
    char c ='M';


    for(int i=4;i<ch.length;i++){
        boolean b1 = false;
        boolean b2 = false;
        boolean b3 = false;
        boolean b4 = false;
        c = ch[i];
        if((c >= '0' && c <= '9') && (ch[i-1]=='\n')){
            b1 = true;

            if((ch[i+1] >= '0' && ch[i+1] <= '9'))b2=true;

            if((ch[i+2] >= '0' && ch[i+2] <= '9'))b3=true;}

        if(b1)ch[i]='Q';
        if(b2)ch[i+1]='Q';
        if(b3)ch[i+2]='Q';

        b1 = false;
        b2 = false;
        b3 = false;
    }

    String strings = ch.toString();
    strings.replace("Q","");

    Log.d("meoww","juy "+strings);

As you can see, the for loop tries to get rid of these numbers for me. The logs show the following output:

juy [C@3d03b1f5

I know its probably a minor error, but how can I correct this. Like I say, I will have to do this about 3600 inside this for loop.

thanks for any suggestions,

Keep on coding

3
  • Use String.valueOf() instead of toString() Commented Aug 18, 2016 at 5:45
  • If it's showing something like [C@3d03b1f5 that means it's displaying the address of the object and not the contents inside...just saying Commented Aug 18, 2016 at 5:49
  • Try String strings = new String(ch); instead of String strings = ch.toString(); Commented Aug 18, 2016 at 6:07

3 Answers 3

1

This code

String.replaceAll ("^\d*\s", " ") this will take numbers at the beginning of the line ^ following by one of more digit \d* followed by a space and replace this with a space

Sign up to request clarification or add additional context in comments.

1 Comment

This probably will do the trick. I will upvote it as soon as i test it out. Hope it doesn't remove crital numbers used in array or for loop construction. @Scary Wombat
0

try this: String str = string.replaceAll("[0-9]","");

This will remove all digits.

3 Comments

It will remove all digits from the strings, so may it remove any digit present in the code also???
It will replace all digits from String with empty values. So yes.
I cant have the digits that are used in new arrays, for loops and alpha-numeric variable names removed. @Vygintas B
0

Try with this:

public static void main(String[] args)
      {// inside onCreate method
        String string = "3    import android.animation.Animator; \n"
            + "4    import android.animation.AnimatorListenerAdapter; \n"
            + "5    import android.animation.ValueAnimator; \n" + "6    import android.app.Activity; \n"
            + "7    import android.content.Context; \n" + "8    import android.content.DialogInterface; \n"
            + "17    import android.content.Context; \n" + "558    import android.content.DialogInterface; \n";

        char[] ch = string.toCharArray();
        if(ch[0] > '0' && ch[0] < '9'){
          ch[0] = ' ';
        }
        for (int i = 0; i < ch.length; i++)
        {
          if (ch[i] == '\n')
          {
            int count = i + 1;
            if (count < ch.length)
            {
              while (count < ch.length && ch[count] >= '0' && ch[count] <= '9')
              {
                ch[count] = ' ';
                count++;
              }
            }
          }
        }

        String strings = new String(ch);

        System.out.println(strings);
      }

This code will replace number at first position of each line with space. After this you can apply eclipse formatter to remove unnecessary spaces.

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.