View Full Version : toString Not Converting Tabs or New Lines?
javacookman
03-03-2004, 10:38 PM
Hello,
Below is a snippet from my code, the toString function is working perfectly except for the fact that it does not replace Tabs or New Lines (and probably others?)
I made a workaround which works fine for the new lines (see the \\n below) but am still confused about the tabs and to if there are other characters also not being replaced?
var newStr = "";
rl = "\\n"+textStream.ReadLine();
for(var i = 0; i < rl.length; i++) {
hc = (rl.charCodeAt(i)).toString(16);
if (hc.length==2) {
newStr += hc;
}
}
Any help is much appreciated, thanks!
javacookman
03-04-2004, 02:35 AM
Is there some replace I have to do with \t or something?
glenngv
03-04-2004, 03:37 AM
try:
r1 = r1.replace(/\t|\n/g,'');
javacookman
03-04-2004, 04:01 AM
Ummm... Doesn't that remove all tabs and new lines?
I want to get the hex codes for them because they are all ready automatically removed.
glenngv
03-04-2004, 04:17 AM
That removes all the tabs and newlines. Isn't it you were asking for or I misunderstood your question?
javacookman
03-04-2004, 05:01 AM
I think you misunderstood.
I'm using toString right now in this function:
var newStr = "";
rl = "\\n"+textStream.ReadLine();
for(var i = 0; i < rl.length; i++) {
hc = (rl.charCodeAt(i)).toString(16);
if (hc.length==2) {
newStr += hc;
}
}
What it does is replace each character with it's 2 digit hex code. But the problem is, tabs and new lines (I fixed the new lines, but not tabs) aren't being replaced. Do you know how to fix that?
glenngv
03-04-2004, 05:16 AM
I misunderstood you earlier but the solution is still my original suggestion:
var newStr = "";
rl = "\\n"+textStream.ReadLine();
rl = rl.replace(/\t|\n/g,''); //remove tabs and newlines
for(var i = 0; i < rl.length; i++) {
hc = (rl.charCodeAt(i)).toString(16);
newStr += hc;
}
By removing the tabs and newlines before converting each character to hex, you won't need the checking anymore in order to exclude them.
Am I correct now?
javacookman
03-04-2004, 05:34 AM
But when I reverse the toString, the tabs and new lines will be gone, I need them still there, so they have to be hexed too. Does that make sense?
Thanks.
glenngv
03-04-2004, 05:54 AM
So why do you exclude them in the first place?
javacookman
03-04-2004, 06:18 AM
What do you mean? I don't? Do I?
javacookman
03-04-2004, 06:36 AM
Bah! I'm such a moron, haha.
I get what you mean now. :)
I fixed my code so instead of stripping single characters, it just adds a 0 before it. ;)
Thanks a lot for all your help!
glenngv
03-04-2004, 06:36 AM
What is this for?
if (hc.length==2) {
newStr += hc;
}
If you will describe the purpose behind this script, maybe our minds will meet somewhere.
javacookman
03-04-2004, 06:39 AM
Just a note, here's the updated code...
var newStr = "";
rl = "\n"+textStream.ReadLine();
for(var i = 0; i < rl.length; i++) {
hc = (rl.charCodeAt(i)).toString(16);
if (hc.length==1) {
newStr += "0"+hc;
} else {
newStr += hc;
}
}
Originally posted by glenngv
If you will describe the purpose behind this...
http://www.codingforums.com/showthread.php?&threadid=34403
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.