This could very well be the most noobish question EVER

Discussion in 'Programming' started by Site Owner, Aug 2, 2007.

  1. #1
    I have this string of text: "Hello my name is Andy Hello my name is John Hello my name is Matt"

    I want this:
    Hello my name is Andy
    Hello my name is John
    Hello my name is Matt

    How do I do this in Javascript? :eek:
     
    Site Owner, Aug 2, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Not the most elegant way I'm sure but this works: :D
    string = "Hello my name is Andy Hello my name is John Hello my name is Matt";
    while (string.indexOf(" Hello") != -1)
      string = string.replace(" Hello", "\nHello");
    alert(string);
    
    Code (markup):
    It finds "Hello" with a space before it and replaces the space with "\n" (\n represents a new line)
     
    krt, Aug 2, 2007 IP
  3. Jordan Matthews

    Jordan Matthews Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the easiest answer is to put a \n in between the phrases manually if you're allowed. that's the code for a newline.

    Alternatively, you could set up a function that adds a new line after every 5th word.
     
    Jordan Matthews, Aug 2, 2007 IP