3 Replies Latest reply on Dec 14, 2006 3:28 PM by trigger2160

    Replacing 2 or more blank characters with 1

    trigger2160
      Hi. I have created a search function which takes some text a user types in and then searches through some dynamic text for them. If any of these words are found then they are highlighted red. this works fine if a user types in a word, space, another word etc. but if a user were to enter 2 spaces or more between a word then it will crash. What i want to do is take the text a user types in, set all blank characters between words to 1, before the words are serched for. Is there any way to search through a text string for occurrences of more than 1 blank character and then remove them so that only 1 blank character remains.
        • 1. Re: Replacing 2 or more blank characters with 1
          Level 7

          // strip out extra spaces between words
          var s:String = "Howdy, bubba! There's oil in them thar hills!!";
          trace(s);
          var arr:Array = s.split(" ");
          for(var i=arr.length-1; i>-1; i--){
          if (arr .length < 1 || arr==undefined){arr.splice(i,1);}
          }
          trace(arr.join(" "))



          // strip out extra spaces between words (multi-line version)

          var s:String = "Howdy, bubba!\n There's oil in them thar hills!!";
          var lines:Array = s.split("\n");
          for(var i=0; i<lines.length; i++){
          var arr:Array = lines .split(" ");
          for(var j=arr.length-1; j>-1; j--){
          if (arr[j].length < 1 || arr[j]==undefined){arr.splice(j,1);}
          }
          lines
          = arr.join(" ");
          }
          trace(lines.join("\n"))


          • 2. Re: Replacing 2 or more blank characters with 1
            injpix Level 3
            I am not sure if Basque's code is what you are looking for however I came up with this code that will make any multiple-space in a text into a single-space. Just copy and paste the code in a frame and notice how my_txt changes. Note; this is sloppiest code I posted here in awhile but it gets the job done! I can help you with it later on in the week when I have more time. I hope this helps.
            • 3. Re: Replacing 2 or more blank characters with 1
              trigger2160 Level 1
              thank u both for ur reply. i tried both of ur pieces of code but neither did what i wanted. but they did help me a lot to write my own method. it seems to work fine. thanks for ur help. if u r wondering, this is the code i used.

              var inText = inputText.text;
              var finished = false
              var startPos = 0;
              while (!finished) {
              //find first occurence of space
              startPos = inText.indexOf(" ", startPos);
              //if a space exists
              if (startPos != -1) {
              //see if there is a space after the other one
              var nextPos = inText.indexOf(" ", startPos+1);
              if (nextPos == startPos+1) {
              //if there is then remove it
              var newString = inText.substring(0, startPos);
              newString += inText.substring(nextPos, inText.length);
              inText = newString;
              } else {
              //otherwise carry on looking
              startPos += 1;
              }
              } else {
              //now that the whole string has been searched,
              //check to see if a space remains at the end
              var endSpace = inText.indexOf(" ", inText.length-1)
              if(endSpace != -1){
              //if it does remove it
              var newString = inText.substring(0, inText.length-1);
              inText = newString;
              }
              finished = true;
              }
              }
              inputText.text = inText;