【发布时间】:2013-03-21 05:26:42
【问题描述】:
我正在 android 中创建 CSV 文件。文件已成功创建,但如果内容中有换行符,则内容格式会受到干扰。 请建议如何处理换行符。
【问题讨论】:
我正在 android 中创建 CSV 文件。文件已成功创建,但如果内容中有换行符,则内容格式会受到干扰。 请建议如何处理换行符。
【问题讨论】:
如果可以提供有关如何接收和处理数据的更多详细信息,则可以提供一个更详细的示例,说明如何将替换项适合您的方法。
您应该使用 CSV 文件中更易于处理的内容替换新行字符。我建议使用 2 个字符的字符串“\n”,但您也可以将其替换为空白、破折号或完全删除该字符。
当您使用它时,您可能还想处理换行符“\r”和制表符“\t”。请务必转义反斜杠字符。
示例(伪代码):
StringBuffer s = new StringBuffer();
char c;
loop-through-characters {
if(c == '\n') {
// Replace new-line with "\n".
s.append("\\n");
} else if(c == '\r') {
// Replace line-feed with "\r".
s.append("\\r");
} else if(c == '\t') {
// Replace tab with space " ".
s.append(" ");
} else {
// Otherwise, append the character as-is.
s.append(c);
}
}
// Process the bytes[] in StringBuffer s as you would have
// previously processed the raw bytes[].
【讨论】: