2016-08-18 2 views
0

특정 문자열의 서식을 지정하려고합니다. 나는 언젠가 저에게 노트북의 불행한 사건이 발생하여 많은 가치있는 코드를 삭제했습니다. 나는 대부분의 파일을 저장했지만 저장되지 않은 몇 가지는 안드로이드 앱에 매우 중요했습니다. 그럼 여기에 문자열을 포맷하려고하는 코드가 있습니다. 각 줄의 시작 부분에있는 모든 숫자가 없어야합니다. 약 3600 명이 있습니다. Android Studio에서 JAVA로 문자열 서식 지정

//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); 

당신이 볼 수 있듯이

는 루프의 나를 위해이 숫자를 제거하려고합니다. 로그는 다음과 같은 출력 보여 :
juy [[email protected] 

가 나는 그것 아마 사소한 오류를 알고,하지만 나는이 문제를 어떻게 해결할 수 있습니다. 내가 말했듯이, 나는 이것을 for 루프 안에서 약 3600 번해야 할 것이다. 어떤 제안에 대한

덕분에,

+0

사용'한 String.valueOf 이것을 대체 할

String.replaceAll ("^\d*\s", " ")이 코드)' toString() 대신에 – Jens

+0

'[C @ 3d03b1f5']와 같은 내용이 표시되면 내용이 아닌 객체의 주소가 표시됩니다 ... 그냥 – bholagabbar

+0

이라고 말하면됩니다. 대신 'String strings = new String (ch); 'String strings = ch.toString(); ' –

답변

0

이 시도 코딩에 보관은 : String str = string.replaceAll("[0-9]","");

이 모든 숫자를 제거합니다.

+0

문자열에서 모든 자릿수를 제거하므로 임의의 자릿수를 제거 할 수 있습니다 코드에 선물도 ??? –

+0

String의 모든 자릿수를 빈 값으로 대체합니다. 그래. –

+0

새 배열에서 사용되는 숫자는 루프 및 영숫자 변수 이름에 대해 제거 할 수 없습니다. @Vygintas B –

0

이 함께 시도 :

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); 
     } 

이 코드는 공간이 각 행의 첫 번째 위치에 번호를 대체합니다. 그런 다음 불필요한 공백을 제거하기 위해 Eclipse 형식기를 적용 할 수 있습니다.

1

이 (뒤에 공백이 더 자리 \d* 중 하나에 의해 다음과 같은 라인 ^의 시작 부분에 숫자를 가지고 공간

+0

이것은 받아 들여야합니다 +1 +1 –

+0

이것은 아마도 트릭을 할 것입니다. 나는 그것을 테스트하자마자 그것을 upvote 것입니다. 배열이나 루프 생성에 사용되는 임계 수를 제거하지 않기를 바랍니다. @ Skary Wombat –