English 中文(简体)
在文件中添加逗号。
原标题:
  • 时间:2008-12-29 12:48:40
  •  标签:

This is my first question in this forum... I have a file with numerous data fields (both numeric and characters) in the file. I want to delimit the file with set of delimiter length like 2,5,1,9,6 etc.

或者:我有一个字段长度列表,例如2、5、1、9、6,我想在每个字段后面在(源字符串的副本中)插入逗号分隔符。

例如,如果我的文件是这样的:

9483trdcvge245621jde

then I need to insert commas at 2,5,1,9,6 etc. and the output will be:

94,83trd,c,vge245621,jde,

我需要用JAVA来做这件事。

Please help me to solve this issue. Thanks in advance

问题回答
if (myString.equals("9483trdcvge245621jde")) {
        myString = "94,83trd,c,vge245621,jde";
    }

开玩笑;-)

我想大概是这个样子的...

private static final int[] pos = {2, 5, 1, 9, 6};
private static final String DIV = ",";

public static String parse(String str) {
    int start = 0;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < pos.length; i++) {
        if (i > 0) { 
            sb.append(DIV);
        }
        int end = start + pos[i];
        if (end <= str.length()) {
            sb.append(str.substring(start, end));
            start = end;
        } else {
            sb.append(str.substring(start));
        }

    }
    return sb.toString();
}

将文件读入StringBuilder,然后使用类似以下的东西。

StringBuilder sb = new StringBuilder(file); //The string builder
while (/*the string builder isn t finished*/)
{
  int position = ;//The position you want the comma at 2 or 4 or whatever
  sb.insert(position, ",");
}

循环尽可能多的次数。

我觉得我会这样做。

  • str being the input string
  • pos being the lengths of the parts after which we should put a comma

代码:

public static String partition(String str, int[] pos) {
    int oldPos = 0;
    StringBuilder builder = new StringBuilder(str.length() + pos.length);
    for(int len : pos) {
        builder.append(str.substring(oldPos, oldPos+len)).append( , );
        oldPos += len;
    }
    builder.append(str.substring(oldPos)).append( , );
    return builder.toString();
}

我觉得我不理解这个问题。逐行阅读文件,并将逗号插入字符串中。

String newString = line.substring(0, firstComma) + "," + line.substring(firstComma + 1);

当然,这是非常低效的,有很多方式可以进行优化。

假设您拥有所有这些内容作为字符串,您可以使用String.substring(start,end)。 然后将子字符串和逗号简单地附加到一起。

String data = "9483trdcvge245621jde";
String result = "";

result += data.substring(0,2) + ",";
result += data.substring(2, 7) + ",";
result += data.substring(7, 8) + ",";

等等...

注意:像这样使用+附加字符串非常缓慢,因为它每次都重新分配和移动数据。如果速度是问题,有更快的方法来连接String

String newString = "";
int[] positions = { 2, 5, 1, 9, 6 }; //etc
for (int i = 0; i > positions.length; i++) {
    String tempString = "";
    if (i == positions.length) { //for the last item
      tempString = oldString.substring(0, positions[i]);
    }
    else { //every item except the last item
      tempString = oldString.substring(0, positions[i]) + ",";
    }
    oldString = oldString.substring(positions[i]);
    newString += tempString;
}

Stored the positions in an array. Iterate through, adding the delimited strings to a new string and removing them from the old one. This might not be the best way, but its how I would do it. :P

这是一个解决方案:

package com.foo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Commafy {

    public static final String SEPARATOR = ",";

    private static void fail(String message) {
        System.err.println(message);
        System.exit(1);
    }

    private static int[] argsAsInts(String[] args) {
        if (args.length < 2) {
            fail("argument list of file name followed by field lengths is required");
        }
        int[] result = new int[args.length - 1];
        for (int i = 1; i < args.length; ++i) {
            try {
                result[i - 1] = Integer.parseInt(args[i]);
            } catch (NumberFormatException nfe) {
                fail("can t convert argument "" + args[i] + "" to integer");
            }
        }
        return result;
    }

    private static int[] partialSums(int[] lengths) {
        int[] result = new int[lengths.length];
        int start = 0;
        for (int i = 0; i < lengths.length; ++i) {
            result[i] = start;
            start += lengths[i];
        }
        return result;
    }

    private static int[] fieldsEndAt(int[] lengths, int[] starts) {
        int[] result = new int[lengths.length];
        for (int i = 0; i < lengths.length; ++i) {
            result[i] = starts[i] + lengths[i];
        }
        return result;
    }

    private static void process(
        String fileName, int[] starts, int[] ends
    ) throws IOException {
        BufferedReader br = new BufferedReader(
            new FileReader(fileName)
        );
        final int MIN_LENGTH = ends[ends.length - 1];
        String line = br.readLine();
        while (line != null) {
            if (line.length() < MIN_LENGTH) {
                System.err.println("short input line "" + line +"" skipped");
            } else {
                StringBuilder sb = new StringBuilder();
                String separate = "";
                for (int i = 0; i < starts.length; ++i) {
                    sb.append(separate).append(line.substring(starts[i], ends[i]));
                    separate = SEPARATOR;
                }
                System.out.println(sb.toString());
            }
            line = br.readLine();
        }
        br.close();
    }

    public static void main(String[] args) {
        int[] lengths = argsAsInts(args);
        int[] starts = partialSums(lengths);
        int[] ends = fieldsEndAt(lengths, starts);
        try {
            process(args[0], starts, ends);
        } catch (IOException e) {
            fail("I/O Exception while processing input");
        }
    }

}

给定数据文件名data/fixedlengthdata.text,其中包含:

9483trdcvge245621jde
9483trdcvge245621jdelong
9483trdcvge245621
9483trdcvge245621jde

并运行参数:

data/fixedlengthdata.text 2 5 1 9 3

它产生输出:

94,83trd,c,vge245621,jde
94,83trd,c,vge245621,jde
short input line "9483trdcvge245621" skipped
94,83trd,c,vge245621,jde

(当然,上面的第三行输出到stderr。)

这可能是我见过最奇怪的需求,但无论如何...

伪代码

Collection<Integer> indexes; // initialized with indexes to add commas at 
StringBuilder bldr = new StringBuilder();

for (int i = 0; i < inString.length(); i++){

     bldr.append(inString.charAt(i));

     if (indexes.contains(i))
         bldr.append(",");

}

return bldr.toString();




相关问题
热门标签