write part of a string ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • find clausen

    write part of a string ?


    How do I write part of a string up to a certain character,
    or get it in to a variable.

    TIA

    --

    find clausen

  • Lasse Reichstein Nielsen

    #2
    Re: write part of a string ?

    find clausen <> writes:
    [color=blue]
    > How do I write part of a string up to a certain character,
    > or get it in to a variable.[/color]

    I am not sure exactly what you want to do.

    Do you want to change the contents of a string (up to a point)? In
    that case, you can't. Javascript strings are immutable. You have to create
    a new string, e.g.:

    str = "dummy string";
    str = "new prefix "+str.substring (6);
    alert(str); // alerts "new prefix string"

    Get it into a variable? I can't guess that one.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • kaeli

      #3
      Re: write part of a string ?

      In article <lls3lv9z.fsf@h otpop.com>, lrn@hotpop.com enlightened us
      with...[color=blue]
      >
      > Get it into a variable? I can't guess that one.
      >[/color]

      Sure you can. :)

      str="new prefix";
      myNewVar = str.substring(4 );

      myNewVar is now "prefix".


      -------------------------------------------------
      ~kaeli~
      All I ask for is the chance to prove that money
      cannot make me happy.


      -------------------------------------------------

      Comment

      • find clausen

        #4
        Re: write part of a string ?

        On 02 Oct 2003 13:43:36 +0200, Lasse Reichstein Nielsen
        <lrn@hotpop.com > wrote:
        [color=blue]
        > Get it into a variable? I can't guess that one.[/color]

        :-)

        I have this:
        fv = document.title. substr(0,20)

        But I want to find @ in document.title
        And put that part of the string in fv

        --

        find clausen

        Comment

        • kaeli

          #5
          Re: write part of a string ?

          In article <t98onvsa7hnogr 5qaa7bn6f723pr5 p5m2o@4ax.com>, find clausen <>
          enlightened us with...[color=blue]
          > I have this:
          > fv = document.title. substr(0,20)
          >
          > But I want to find @ in document.title
          > And put that part of the string in fv
          >
          >[/color]

          Which part of the string? The part from 0 to '@' or the part from '@' to
          the end?
          Inclusive or exclusive of the '@'?

          --
          -------------------------------------------------
          ~kaeli~
          All I ask for is the chance to prove that money
          cannot make me happy.


          -------------------------------------------------

          Comment

          • W d'Anjos

            #6
            Re: write part of a string ?

            Use the subtr method:

            <SCRIPT LANGUAGE="JavaS cript1.2">
            str = "abcdefghij "
            document.writel n("(1,2): ", str.substr(1,2) )
            document.writel n("(-2,2): ", str.substr(-2,2))
            document.writel n("(1): ", str.substr(1))
            document.writel n("(-20, 2): ", str.substr(1,20 ))
            document.writel n("(20, 2): ", str.substr(20,2 ))
            </SCRIPT>

            This script displays:

            (1,2): bc
            (-2,2): ij
            (1): bcdefghij
            (-20, 2): bcdefghij
            (20, 2):

            More info at:


            -Wagner

            find clausen <> wrote in message news:<ulonnv8jv v1i7s1kdshm1855 9nkc9dqh4h@4ax. com>...[color=blue]
            > How do I write part of a string up to a certain character,
            > or get it in to a variable.
            >
            > TIA[/color]

            Comment

            • find clausen

              #7
              Re: write part of a string ?

              On Thu, 2 Oct 2003 09:38:46 -0500, kaeli
              <infinite.possi bilities@NOSPAM att.net> wrote:
              [color=blue]
              > Which part of the string? The part from 0 to '@' or the part from '@' to
              > the end?
              > Inclusive or exclusive of the '@'?[/color]

              from 0 to @ -1

              --

              find clausen

              Comment

              • kaeli

                #8
                Re: write part of a string ?

                In article <aononvkdcvhosl l8tj1ng115vf43h rrua8@4ax.com>, find clausen <>
                enlightened us with...[color=blue]
                > On Thu, 2 Oct 2003 09:38:46 -0500, kaeli
                > <infinite.possi bilities@NOSPAM att.net> wrote:
                >[color=green]
                > > Which part of the string? The part from 0 to '@' or the part from '@' to
                > > the end?
                > > Inclusive or exclusive of the '@'?[/color]
                >
                > from 0 to @ -1
                >
                >[/color]

                fv = document.title. substring(0,doc ument.title.ind exOf("@"))

                Note on substr/substring: substr() has two arguments - the start and the
                length. substring() has two arguments - the start and the end.


                -------------------------------------------------
                ~kaeli~
                All I ask for is the chance to prove that money
                cannot make me happy.


                -------------------------------------------------

                Comment

                • find clausen

                  #9
                  Re: write part of a string ?

                  On Thu, 2 Oct 2003 12:51:08 -0500, kaeli
                  <infinite.possi bilities@NOSPAM att.net> wrote:[color=blue]
                  > fv = document.title. substring(0,doc ument.title.ind exOf("@"))
                  >
                  > Note on substr/substring: substr() has two arguments - the start and the
                  > length. substring() has two arguments - the start and the end.[/color]

                  That works perfect, thanx !

                  Then I will not pay you :[color=blue]
                  > All I ask for is the chance to prove that money
                  > cannot make me happy.[/color]
                  ;-)
                  --

                  find clausen

                  Comment

                  • find clausen

                    #10
                    Re: write part of a string ?

                    On 2 Oct 2003 10:15:47 -0700, wanjos@yahoo.co m (W d'Anjos) wrote:
                    [color=blue]
                    > Use the subtr method:
                    >
                    > <SCRIPT LANGUAGE="JavaS cript1.2">
                    > str = "abcdefghij "
                    > document.writel n("(1,2): ", str.substr(1,2) )
                    > document.writel n("(-2,2): ", str.substr(-2,2))
                    > document.writel n("(1): ", str.substr(1))
                    > document.writel n("(-20, 2): ", str.substr(1,20 ))
                    > document.writel n("(20, 2): ", str.substr(20,2 ))
                    > </SCRIPT>
                    >
                    > This script displays:
                    >
                    > (1,2): bc
                    > (-2,2): ij
                    > (1): bcdefghij
                    > (-20, 2): bcdefghij
                    > (20, 2):[/color]

                    Thanx, I will save this !

                    --

                    find clausen

                    Comment

                    • Dr John Stockton

                      #11
                      Re: write part of a string ?

                      JRS: In article <ulonnv8jvv1i7s 1kdshm18559nkc9 dqh4h@4ax.com>, seen in
                      news:comp.lang. javascript, find clausen <?@?.?> posted at Thu, 2 Oct
                      2003 10:36:10 :-[color=blue]
                      >
                      >How do I write part of a string up to a certain character,
                      >or get it in to a variable.[/color]

                      Let the character be #, and the string example be S = 'abcd#xyz'

                      S.replace(/#.*/, '') // -> 'abcd'
                      S.replace(/.*#/, '') // -> 'xyz'

                      OK = /(.*)#(.*)/.test(S)
                      // RegExp.$1 = 'abcd'
                      // RegExp.$2 = 'xyz'

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                      Comment

                      Working...