java分割令人困惑的空格字符
我正在分割一个包含 Windows 系统文件名的字符串。该字符串使用 ascii FS 将文件名与其他信息分开,
例如filename.jpgFSotherInformationFSanotherPartOfInformation
这里有一些示例代码:
String fs = new String(new byte[]{(byte)32});
String information ="filename (copy).jpg"+fs+"otherInformation";
String[] parts = information.split(fs);
为什么 split 将空格分隔符与 ascii-FS 混淆?
我应该使用不同的分割函数吗? Pattern.quote(fs) 确实有帮助...:-(
I am splitting a string which contains a filename from a windows system. The string uses the ascii FS to separate the filename from other information
e.g. filename.jpgFSotherInformationFSanotherPartOfInformation
Here some example code:
String fs = new String(new byte[]{(byte)32});
String information ="filename (copy).jpg"+fs+"otherInformation";
String[] parts = information.split(fs);
Why does split confuse the space-separator with the ascii-FS?
Should I use a different function that split? Pattern.quote(fs) does help either... :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为 FS 不是 ascii 值 32。
http://bestofthisweb.com/blogs/tag/ascii- table/
FS
是字符 28,但是这个控制字符不应该在文件名中使用,仅适用于一些罕见的二进制文件格式(我不知道还有哪个使用它) )空格字符是 32,这就是为什么它看起来与分割相同,因为它确实如此。
对于简单的字段分隔符,我建议您使用“,”或“\t”,它们可以轻松地作为文本读取或使用电子表格包。
我建议在调试器中单步执行代码,以便您可以看到程序正在做什么。
Because FS is not ascii value 32.
http://bestofthisweb.com/blogs/tag/ascii-table/
The
FS
is character 28, but this control character should not be used in file names, only for some rare binary file formats (I don't know of one which uses it anymore)The space character is 32 which is why it looks the same the split, because it is.
For a simple field seperator, I suggest you use ',' or '\t' which can be easily read as text or using a spreadsheet package.
I would suggest stepping through the code in a debugger so you can see what you program is doing.
您已经用空格初始化了
fs
(以相当复杂的方式)。以下内容相同并显示了您的问题:ascii 字符
FS
的编号为0x1C
,因此应该可以正常工作:背景信息
FS
的发明是为了在分层文件目录中分隔真实的文件而不是文件名。从技术上讲,是的,你可以使用它,但它有不同的含义。You've initialized
fs
with a space (in a rather complicated way). The following is equal and shows your problem:The ascii char
FS
has the number0x1C
, so this should work properly:Background information
The
FS
was invented to separate real files and not filenames in a hierarchical file directory. Technically, yes, you can use it, but it has a different meaning.因为 FS 是 Ascii 值 28
Ascii 值 32 是
空格
Beacuse FS is Ascii values 28
Ascii value 32 is
space
split的参数实际上是一个正则表达式,你尝试过
甚至
Split's parameter is actually a regular expression, have you tried
Or even