www.webdeveloper.com
Results 1 to 5 of 5

Thread: string manipulation

  1. #1
    Join Date
    Jul 2006
    Posts
    128

    string manipulation

    i have a string of the following type:
    someText||otherText|relatedText||differentText|relatedText

    I know that somewhere in that string is the information "myText|(myRelatedText)". How do I split apart the string using the || splits, and then search the different chunks for "myText"?
    In php, I would use str_split and then indexOf (to see if the chunk contained "myText")....are there any related functions in javascript that I can use?
    thanks

  2. #2
    Join Date
    Jul 2006
    Posts
    128
    ok, found the answer through searching...String.split and String.indexOf ....not very different :P

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,350
    Glad you could solve it by yourself. I guess I just too slow of a typist!

    Consider this:
    PHP Code:
    <html>
    <
    head>
    <
    title>Split Fields</title>
    <
    script type="text/javascript">
    <!--
    var 
    str 'someText||otherText|relatedText||differentText|relatedText';

    function 
    StrSplits() {
      var 
    strflds str.split('||');
      var 
    flds '';
      for (var 
    i=0i<strflds.lengthi++) { flds += '['+i+']: '+strflds[i]+"\n"; }
      
    alert('str split at ||\n'+flds);
    }

    function 
    StrSplitter() {
      var 
    strflds str.split('|');
      var 
    flds '';
      for (var 
    i=0i<strflds.lengthi++) { flds += '['+i+']: '+strflds[i]+"\n"; }
      
    alert('str split at |\n'+flds);
    }

    //-->
    </script>
    </head>
    <body>
    <h1>String Splitter</h1>
    str = 'someText||otherText|relatedText||differentText|relatedText'
    <p />
    <button onClick="StrSplits()">Split at ||</button>
    <button onClick="StrSplitter()">Split at |</button>

    </body>
    </html> 

  4. #4
    Join Date
    Jul 2006
    Posts
    128
    a much more elegant solution....(i need to start using functions more often)...thanks for the help though

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,350
    You're welcome. I'll try to be quicker on the response the next time.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles