Web Analytics Made Easy -
StatCounter toString Not Converting Tabs or New Lines? - CodingForum

Announcement

Collapse
No announcement yet.

toString Not Converting Tabs or New Lines?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • toString Not Converting Tabs or New Lines?

    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?

    Code:
    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!

  • #2
    Is there some replace I have to do with \t or something?

    Comment


    • #3
      try:

      r1 = r1.replace(/\t|\n/g,'');
      Glenn
      vBulletin Mods That Rock!

      Comment


      • #4
        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.

        Comment


        • #5
          That removes all the tabs and newlines. Isn't it you were asking for or I misunderstood your question?
          Glenn
          vBulletin Mods That Rock!

          Comment


          • #6
            I think you misunderstood.

            I'm using toString right now in this function:
            Code:
            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?

            Comment


            • #7
              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?
              Glenn
              vBulletin Mods That Rock!

              Comment


              • #8
                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.

                Comment


                • #9
                  So why do you exclude them in the first place?
                  Glenn
                  vBulletin Mods That Rock!

                  Comment


                  • #10
                    What do you mean? I don't? Do I?

                    Comment


                    • #11
                      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!

                      Comment


                      • #12
                        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.
                        Glenn
                        vBulletin Mods That Rock!

                        Comment


                        • #13
                          Just a note, here's the updated code...

                          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;
                          }
                          }

                          Comment


                          • #14
                            Originally posted by glenngv
                            If you will describe the purpose behind this...
                            hmm... ?

                            Comment

                            Working...
                            X