在文本文件中的逗号之间提取空白字 [英] Extract blank word between comma in text file

查看:73
本文介绍了在文本文件中的逗号之间提取空白字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图获取包含文本文件中的逗号和打印到日志文件之间的空字的记录



例如:



巴黎,美国,西班牙,日本

印度,斯里兰卡,巴基斯坦,孟加拉国



我将在日志中获得输出:巴黎,美国,西班牙,因为下面的逗号之间的空格是我的代码。



来自代码我不明白为什么数组没有检测到null eventHough我已经与(,)分开。

请帮助我。



我尝试过:



while((s = sr.readLine())!= null){

PrintStream p = new PrintStream(Log_file);

String a [] = s.split(,);

//系统。 out.println(s);

//System.out.println(a[5 ]);

for(String k:a)

{

if(k!= null)

{

//System.out.prin TLN(A [1]);

if(a [i] == null){

p.println(s);



I was trying to get the record which contains null word between comma from text file and printing to log file

for example :

paris,, America, Spain, Japan
india,Srilanka,Pakistan,Bangladesh

I will get the output in log: Paris, ,America, Spain, because getting null between comma below is my code .

from code i dont understand why array not detecting null eventhough i have split with (,).
pls help me out .

What I have tried:

while((s = sr.readLine())!= null){
PrintStream p = new PrintStream(Log_file);
String a[]= s.split(",");
// System.out.println(s);
//System.out.println(a[5]);
for(String k:a)
{
if(k !=null)
{
//System.out.println(a[i]);
if(a[i] ==null){
p.println(s);

推荐答案

a 中不会有 null 值。如果两个逗号之间没有任何内容,则 a 中的结果将是空字符串而不是 null 。而不是检查null,检查字符串是否为空, k.isEmpty()
There won't be null values in a. If there is nothing between two commas, the result in a will be an empty string rather than null. Instead of checking for null, check if the string is empty, with k.isEmpty().


这篇关于在文本文件中的逗号之间提取空白字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆