Java 删除多行开头的整数
我有一个Java 删除多行开头的整数,java,string,integer,Java,String,Integer,我有一个txt文件,有超过千行的文本,开头有一些整数。 比如: 我想将所有行存储在另一个文件中,不带整数。 我写的代码是: while (s.hasNextLine()){ String sentence=s.nextLine(); int l=sentence.length(); c++; try{//printing P FileOutputStream ffs = new FileOutputStream ("ps.txt",true);
txt
文件,有超过千行的文本,开头有一些整数。
比如:
我想将所有行存储在另一个文件中,不带整数。
我写的代码是:
while (s.hasNextLine()){
String sentence=s.nextLine();
int l=sentence.length();
c++;
try{//printing P
FileOutputStream ffs = new FileOutputStream ("ps.txt",true);
PrintStream p = new PrintStream ( ffs );
for (int i=0;i<l;i++){
if ((int)sentence.charAt(i)<=48 && (int)sentence.charAt(i)>=57){
p.print(sentence.charAt(i));
}
}
p.close();
}
catch(Exception e){}
}
while(s.hasNextLine()){
字符串语句=s.nextLine();
int l=句子长度();
C++;
请尝试打印{//P
FileOutputStream ffs=新的FileOutputStream(“ps.txt”,true);
打印流p=新打印流(ffs);
对于(int i=0;i,代码中有一些地方需要改进:
不要每行都重新打开输出文件,只要一直保持打开状态即可
您正在删除所有数字,而不仅仅是开始时的数字-这是您的意图吗
你知道同时是=57
的数字吗
Scanner.nextLine()
不包括行返回,因此您需要在每行之后调用p.println()
试试这个:
// open the file once
FileOutputStream ffs = new FileOutputStream ("ps.txt");
PrintStream p = new PrintStream ( ffs );
while (s.hasNextLine()){
String sentence=s.nextLine();
int l=sentence.length();
c++;
try{//printing P
for (int i=0;i<l;i++){
// check "< 48 || > 57", which is non-numeric range
if ((int)sentence.charAt(i)<48 || (int)sentence.charAt(i)>57){
p.print(sentence.charAt(i));
}
}
// move to next line in output file
p.println();
}
catch(Exception e){}
}
p.close();
//打开文件一次
FileOutputStream ffs=新的FileOutputStream(“ps.txt”);
打印流p=新打印流(ffs);
而(s.hasNextLine()){
字符串语句=s.nextLine();
int l=句子长度();
C++;
请尝试打印{//P
对于(int i=0;i,代码中有一些地方需要改进:
不要每行都重新打开输出文件,只要一直保持打开状态即可
您正在删除所有数字,而不仅仅是开始时的数字-这是您的意图吗
你知道同时是=57
的数字吗
Scanner.nextLine()
不包括行返回,因此您需要在每行之后调用p.println()
试试这个:
// open the file once
FileOutputStream ffs = new FileOutputStream ("ps.txt");
PrintStream p = new PrintStream ( ffs );
while (s.hasNextLine()){
String sentence=s.nextLine();
int l=sentence.length();
c++;
try{//printing P
for (int i=0;i<l;i++){
// check "< 48 || > 57", which is non-numeric range
if ((int)sentence.charAt(i)<48 || (int)sentence.charAt(i)>57){
p.print(sentence.charAt(i));
}
}
// move to next line in output file
p.println();
}
catch(Exception e){}
}
p.close();
//打开文件一次
FileOutputStream ffs=新的FileOutputStream(“ps.txt”);
打印流p=新打印流(ffs);
而(s.hasNextLine()){
字符串语句=s.nextLine();
int l=句子长度();
C++;
请尝试打印{//P
对于(int i=0;i,可以将此正则表达式应用于从文件中读取的每一行:
String str = ... // read the next line from the file
str = str.replaceAll("^[0-9]+", "");
正则表达式^[0-9]+
匹配行首任意数量的数字。replaceAll
方法将匹配项替换为空字符串。您可以将此正则表达式应用于从文件中读取的每一行:
String str = ... // read the next line from the file
str = str.replaceAll("^[0-9]+", "");
正则表达式^[0-9]+
匹配行首任意数量的数字。replaceAll
方法将匹配项替换为空字符串。在mellamokb注释的顶部,您应该避免使用“幻数”。不能保证数字会在ASCII码的预期范围内
您可以使用character.isDigit
String value = "22Ahmedabad, AES Institute of Computer Studies";
int index = 0;
while (Character.isDigit(value.charAt(index))) {
index++;
}
if (index < value.length()) {
System.out.println(value.substring(index));
} else {
System.out.println("Nothing but numbers here");
}
String value=“22Ahmedabad,AES计算机研究所”;
int指数=0;
while(Character.isDigit(value.charAt(index))){
索引++;
}
如果(索引
(注:dasblinkenlight发布了一些优秀的正则表达式,可能会更容易使用,但如果你喜欢,regexp会让我的大脑翻过来:p)在mellamokb评论的基础上,你应该避免使用“幻数”。不能保证数字会在预期的ASCII码范围内
您可以使用character.isDigit
String value = "22Ahmedabad, AES Institute of Computer Studies";
int index = 0;
while (Character.isDigit(value.charAt(index))) {
index++;
}
if (index < value.length()) {
System.out.println(value.substring(index));
} else {
System.out.println("Nothing but numbers here");
}
String value=“22Ahmedabad,AES计算机研究所”;
int指数=0;
while(Character.isDigit(value.charAt(index))){
索引++;
}
如果(索引
(Nb dasblinkenlight发布了一些优秀的正则表达式,可能更容易使用,但如果你喜欢,regexp会让我的大脑翻过来:p)我想删除所有的数字。我的文本文件只在每行的开头有数字,我想删除。哈哈…我在&&
上挠了一个小时的头。谢谢。我想删除所有的数字。我的文本文件只在每行的开头有数字,我想删除。哈哈…我一直在挠头超过一小时。&谢谢。