8 Replies Latest reply on Jun 3, 2011 2:45 AM by harihisu

    Search index not working in String

    vishalkumar86

      Hello all,

       

           I am using String.search(any sentence) in Flash builder 4. If my sentence to be searched include some special characters such as (), then String.search()

           returns -1 although  that sentence is present in the text. However, if I used to search the same sentence without the special characters, it returns the

           appropriate index. Any help will be greatful.

       

      -Thanks in Advance

       

      Vishal Kumar

        • 1. Re: Search index not working in String
          Flex harUI Adobe Employee

          Please provide a simple but explicit example.

          • 2. Re: Search index not working in String
            vishalkumar86 Level 1

            Hi,

             

            Thanks for the reply,

             

            The text is

            Volcanic ash guidelines are drawn up by the International Civil Aviation Organization, a United Nations body, but experts say there is no commonly agreed safe concentration of ash.
            “The ICAO regulation that has prompted this widespread grounding is from experience gained from over 80 incidents between 1980 and 2000 and computer modeling (or) best guestimate,” said aviation consultant Chris Yates.

             

            Now when I search

            “The ICAO regulation that has prompted this widespread grounding is from experience gained from over 80 incidents between 1980 and 2000 and computer modeling (or) best guestimate,” said aviation consultant Chris Yates.

             

            It returns -1.

             

            However if I remove special characters () and perform search on

            “The ICAO regulation that has prompted this widespread grounding is from experience gained from over 80 incidents between 1980 and 2000 and computer modeling ,

            it returns proper search index .

             

            -Thanks

            Vishal Kumar

             

             

             

             

            • 3. Re: Search index not working in String
              Flex harUI Adobe Employee

              Please show the code that performs this test.

              • 4. Re: Search index not working in String
                vishalkumar86 Level 1

                Hi,

                 

                 

                 

                 

                 

                 

                 

                 

                var index:int = SampleText.text.search("The ICAO regulation that has prompted this widespread grounding is from experience gained from over 80 incidents between 1980 and 2000 and computer modeling");

                 

                returns appropriate index.

                 

                But, when

                var index:int = SampleText.text.search("The ICAO regulation that has prompted this widespread grounding is from experience gained from over 80 incidents between 1980 and 2000 and computer modeling(or) best guestimate,” said aviation consultant Chris Yates.");

                 

                returns -1.

                 

                where, SampleText is mx TextArea.

                 

                -Thanks

                • 5. Re: Search index not working in String
                  _spoboyle Level 4

                  he wants to to supply a runnable test case like this

                   

                  if i understand you correctly this demonstrates your problem

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                              xmlns:s="library://ns.adobe.com/flex/spark" 
                                              xmlns:mx="library://ns.adobe.com/flex/mx"
                                              creationComplete="init()">
                       
                       <fx:Script>
                            <![CDATA[
                                 
                                 [Bindable]
                                 private var text1Index:int = -2;
                                 [Bindable]
                                 private var text2Index:int = -2;
                                 
                                 private function init():void
                                 {
                                      text1Index = text1.text.search("“The ICAO regulation that has prompted this widespread grounding is from experience gained from over 80 incidents between 1980 and 2000 and computer modeling (or) best guestimate,” said aviation consultant Chris Yates.");
                                      text2Index = text2.text.search("“The ICAO regulation that has prompted this widespread grounding is from experience gained from over 80 incidents between 1980 and 2000 and computer modeling (or) best guestimate,” said aviation consultant Chris Yates.");
                                 }
                            ]]>
                       </fx:Script>
                       
                       <s:layout>
                            <s:VerticalLayout/>
                       </s:layout>
                       
                       <s:TextArea id="text1"
                                      width="100%"
                                      text="Volcanic ash guidelines are drawn up by the International Civil Aviation Organization, a United Nations body, but experts say there is no commonly agreed safe concentration of ash. “The ICAO regulation that has prompted this widespread grounding is from experience gained from over 80 incidents between 1980 and 2000 and computer modeling (or) best guestimate,” said aviation consultant Chris Yates."/>
                       
                       <s:TextArea id="text2"
                                      width="100%"
                                      text="Volcanic ash guidelines are drawn up by the International Civil Aviation Organization, a United Nations body, but experts say there is no commonly agreed safe concentration of ash. “The ICAO regulation that has prompted this widespread grounding is from experience gained from over 80 incidents between 1980 and 2000 and computer modeling or best guestimate,” said aviation consultant Chris Yates."/>
                       
                       <s:HGroup>
                            <s:Label text="text1 result:"/>
                            <s:Label text="{text1Index}"/>
                       </s:HGroup>
                       <s:HGroup>
                            <s:Label text="text2 result:"/>
                            <s:Label text="{text2Index}"/>
                       </s:HGroup>
                       
                  </s:WindowedApplication>
                  
                  
                  • 6. Re: Search index not working in String
                    vishalkumar86 Level 1

                    Hi,

                     

                    This clearly depicts my problem.Thanks for the help spoboyle.

                     

                    So, how to handle this issue.

                     

                    -Thanks

                    • 7. Re: Search index not working in String
                      Flex harUI Adobe Employee

                      I think search has a bug where it always assumes regex so you'll need to

                      escape certain characters like \( and \)

                      • 8. Re: Search index not working in String
                        harihisu Level 2

                        It's because the parameter is considered to be a regular expression. To avoid this, just represent the string in RegExp form.

                         

                        <?xml version="1.0" encoding="utf-8"?>
                        <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                                    xmlns:s="library://ns.adobe.com/flex/spark" 
                                                    xmlns:mx="library://ns.adobe.com/flex/mx"
                                                    creationComplete="init()">
                             
                             <fx:Script>
                                  <![CDATA[
                                       
                                       [Bindable]
                                       private var text1Index:int = -2;
                                       [Bindable]
                                       private var text2Index:int = -2;
                                       
                                       private function init():void
                                       {
                                            text1Index = text.text.search("d(");
                                            text2Index = text.text.search(/d\(/);
                                       }
                                  ]]>
                             </fx:Script>
                             
                             <s:layout>
                                  <s:VerticalLayout/>
                             </s:layout>
                        
                             <s:TextArea id="text"
                                            width="100%"
                                            text="abcd(e)efgh"/>
                             
                             <s:HGroup>
                                  <s:Label text="text1 result:"/>
                                  <s:Label text="{text1Index}"/>
                             </s:HGroup>
                             <s:HGroup>
                                  <s:Label text="text2 result:"/>
                                  <s:Label text="{text2Index}"/>
                             </s:HGroup>
                             
                        </s:Application>