多行字符串拆分问题和修复 [英] multiline string split problem and fix

查看:68
本文介绍了多行字符串拆分问题和修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究数据接收系统..我仍然在Javascript中找到我的

方式,虽然我已经完成了很多。


我刚刚解决了一个很难找到的缺陷。症状是

这个:


我从代理+谷歌获得一个多行字符串返回Javascript

Maps API GDownloadUrl( )

添加到DOM表格中的数据看起来很好,大约20行CSV格式

格式


日出, - 119.098,35.345,0.0< br>

SwanDancer,-119.345,35.567,1.0< br>

....等


(我不知道为什么< br>'在那里,但那就是它的样子)

所以使用这个新闻组的建议,我随后执行两个

split()'s

var index,index2;

var strCSVFile = data;

var arrayCSVFile;


arrayCSVFile = strCSVFile.split("< br>");


for( index = 0; index< arrayCSVFile.length; index ++)

{

arrayCSVFile [index] = arrayCSVFile [index] .split('','');

//对元素做东西

}


我使用strCSVFile *和* arrayCSVFile来加倍地确定我不是以某种方式破坏了某些东西,但理论上只需要

原始字符串。无论如何,我看到的就是这个(经过了两个小时的尝试,最后使用了str.charCodeAt())


10 | 32 | 32 | 32 | 32 | 32 | 32 | 32 | 32 | 83 | 117 | 110 | 114 | 105 | 115 | 101 | len = 16

10 | 10 | 32 | 32 | 32 | 32 | 32 | 32 | 32 | 32 | 83 | 119 | 97 | 110 | 68 | 97 | 110 | 99 | 101 | 114 |

len = 20

....等


%^!@#$ ^%@< - 那是'诅咒,人们

所以我现在使用

str.charCodeAt()手动削减一些LF和SPACE字符。最重要的是,我在RegEx

替换方面的偷偷摸摸的尝试一直是沉默失败。可能是因为领先的LF(s)的
。我不知道,花了宝贵的时间..


我寻找split()gotcha',但从未发现过这样的事情。我想b $ b认为我尝试将拆分更改为< BR> \ rr在某一点上,但是我

可能做了返回而不是换行...而且,那不会是
处理第一行的情况?!


这就是现在发生的事情,我现在有了繁琐的代码来处理它。

回顾原始的recv'数据,确实有一个领先的/>
LF | SPACE',每个后续行都有两个LF'。我从未见过它们。

我怎么能这样?当我把他们送到HTML页面检查数据时,他们没有显示

这很糟糕。


FYI

I have been working on a data reception system.. I am still finding my
way around Javascript, though I am accomplishing much.

I just fixed a flaw that was really hard to find. The symptoms are
this:

I get a multiline string returned to Javascript from a Proxy+Google
Maps API GDownloadUrl()
The data, when added to a DOM table looked fine, about 20 lines in CSV
format

Sunrise,-119.098,35.345,0.0<br>
SwanDancer,-119.345,35.567,1.0<br>
.... etc

(I don''t know why the <br>''s are there, but that''s what it looks like)
So using a suggestion from this newsgroup, I perform two subsequent
split()''s

var index, index2;
var strCSVFile = data;
var arrayCSVFile;

arrayCSVFile = strCSVFile.split( "<br>" );

for ( index = 0; index < arrayCSVFile.length; index++ )
{
arrayCSVFile[ index ] = arrayCSVFile[ index ].split( '','' );
// do stuff to the elements
}

I use both strCSVFile *and* arrayCSVFile to be doubly sure I wasn''t
somehow clobbering something, though in theory there needs to be only
the original string. At any rate, what I see is this (after HOURS of
trying and finally using str.charCodeAt())

10|32|32|32|32|32|32|32|32|83|117|110|114|105|115| 101| len=16
10|10|32|32|32|32|32|32|32|32|83|119|97|110|68|97| 110|99|101|114|
len=20
.... etc

%^!@#$^%@ <- that''s cursing, people
So I am now hand clipping some number of LF and SPACE chars using
str.charCodeAt(). On top of that, my furtive attempts at RegEx
replacements along the way had been SILENTLY FAILING. Probably because
of the leading LF(s). I had no idea, and it took valuable time..

I looked for split() gotcha''s but never found anything like this. I
thought I tried changing the split to "<BR>\r" at one point, but I
probably did the return instead of line feed... Also, that would NOT
handle the first line case ?!

This is what is happening, and I now have tedious code to handle it.
Looking back on the original recv''d data, it does indeed have a leading
LF|SPACE''s, with two LF''s on every subsequent row. I never saw them.
How could I ? When I aded them to the HTML page to check the data, they
didn''t show
This was awful.

FYI

推荐答案

^%@< - 那是诅咒,人们

所以我我现在使用

str.charCodeAt()手工剪裁一些LF和SPACE字符。最重要的是,我在RegEx

替换方面的偷偷摸摸的尝试一直是沉默失败。可能是因为领先的LF(s)的
。我不知道,花了宝贵的时间..


我寻找split()gotcha',但从未发现过这样的事情。我想b $ b认为我尝试将拆分更改为< BR> \ rr在某一点上,但是我

可能做了返回而不是换行...而且,那不会是
处理第一行的情况?!


这就是现在发生的事情,我现在有了繁琐的代码来处理它。

回顾原始的recv'数据,确实有一个领先的/>
LF | SPACE',每个后续行都有两个LF'。我从未见过它们。

我怎么能这样?当我把他们送到HTML页面检查数据时,他们没有显示

这很糟糕。


FYI

^%@ <- that''s cursing, people
So I am now hand clipping some number of LF and SPACE chars using
str.charCodeAt(). On top of that, my furtive attempts at RegEx
replacements along the way had been SILENTLY FAILING. Probably because
of the leading LF(s). I had no idea, and it took valuable time..

I looked for split() gotcha''s but never found anything like this. I
thought I tried changing the split to "<BR>\r" at one point, but I
probably did the return instead of line feed... Also, that would NOT
handle the first line case ?!

This is what is happening, and I now have tedious code to handle it.
Looking back on the original recv''d data, it does indeed have a leading
LF|SPACE''s, with two LF''s on every subsequent row. I never saw them.
How could I ? When I aded them to the HTML page to check the data, they
didn''t show
This was awful.

FYI


btw-我刚刚添加了


strCSVFile.replace(/ / g,'''' );

strCSVFile.replace(/ \ n / g,'''');

strCSVFile.replace(/ \ r / g,'''' );


来清理数据(在split()之前的整个区块。我在RegEx中犯了一个

错误吗?他们不知道不行......

btw- I just added

strCSVFile.replace( / /g, '''');
strCSVFile.replace( / \n/g, '''');
strCSVFile.replace( / \r/g, '''');

to clean the data (the whole block before the split()''s. Am I making a
mistake in the RegEx? they don''t work...


>
>

strCSVFile.replace(/ / g,'''');

strCSVFile.replace(/ \ n / g,'''');

strCSVFile.replace(/ \ r / g,'''');

strCSVFile.replace( / /g, '''');
strCSVFile.replace( / \n/g, '''');
strCSVFile.replace( / \r/g, '''');



嗯,非常深夜打字..我的意思是


strCSVFile.replace(/ + / g,'''');

strCSVFile.replace(/ \ n + / g,'''');

strCSVFile。回覆地方(/ \ r + / g,'''');

hmmm, very late night typing.. I meant

strCSVFile.replace( / +/g, '''');
strCSVFile.replace( /\n+/g, '''');
strCSVFile.replace( /\r+/g, '''');


这篇关于多行字符串拆分问题和修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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