[英]Formatting strings in JAVA with Android Studio
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.
我确实保存了大多数文件,但是一些未保存的文件对于我的Android应用程序至关重要。 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.
大约有3600个
//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. 如您所见,for循环试图为我摆脱这些数字。 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.
就像我说的那样,我将不得不在此for循环中执行大约3600次操作。
thanks for any suggestions, 感谢您的任何建议,
Keep on coding 继续编码
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 String.replaceAll ("^\\d*\\s", " ")
它将在第^
行的开头加上数字,再加上一个数字\\d*
后跟一个空格,并将其替换为一个空格
try this: String str = string.replaceAll("[0-9]","");
试试看:
String str = string.replaceAll("[0-9]","");
This will remove all digits. 这将删除所有数字。
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.
之后,您可以使用Eclipse格式化程序删除不必要的空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.