www.webdeveloper.com
Results 1 to 9 of 9

Thread: Choose substring from String

  1. #1
    Join Date
    Aug 2014
    Posts
    3

    Choose substring from String

    Hello everybody,
    I'm new in JS programming.
    I have problem with my thesis at school. So, on website I have one form with input where user can write some String. I have some rules by which I modify specified string. So for example one rule:
    if true then t1 else t2 ---> t1

    Instead of t1 and t2 can user type anything.
    It means, that when I type this String: if true then something else nothing I want to get this String: "something".

    I tried functions like substring() and split(), but it is not for this problem because I don't know what length will have this String.

    Can you help me?

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,402
    Have no idea what you are talking about, the main problem being that you have not posted any of your web form nor any of your script that you have created so far.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Mar 2012
    Posts
    1,720
    @ pista

    I think I understand what you said, but I cannot see any purpose in doing it...

    Why make someone type in "if true then something else nothing" just to extract "something" or "nothing"???

    a) In the first place that means exactly the same as "if something else nothing", which is shorter, quicker and less error prone to type.

    b) What is the condition that is either true or false?

    c) How do you know whether it is true or false?

    d) What purpose does extracting "something" or "nothing" serve???

  4. #4
    Join Date
    Aug 2014
    Posts
    3
    @jedaisoul

    Thank you for your reply.

    At first, it was only example. I try to better explain it.
    In one subject at my school (it is theoretical subject) we have some rules for evaluating expressions. Here are two rules:
    if true then t2 else t3 ---> t2 // it means that when is condition true then is evaluated t2
    if false then t2 else t3 ---> t3 // it means that when is condition false then is evaluated t3

    This subject is composed of those terms and rules, and I make aplication in JS for this subject.

    User must type String in this shape (if true...). I check String from input with this function String.prototype.startsWith(). If String from input starts with "if true" I'll use first rule.

    So I hope that you understand better. I must cut String from input according to rule. Instead of t2 and t3 user writes some expression, which will evaluate with will be evaluated further rules. It's for long I would have to explain this theoretical subject since the early.

    Only I want is cut String from input like I wrote with some rule. When is used this rule if true then t2 else t3 ---> t2 I need to choose substring (which is tagged in rule like t2).

  5. #5
    Join Date
    Mar 2012
    Posts
    1,720
    Thanks for the reply, but as far as I can see, it answers none of my points:

    In logic, propositions are true or false (or neither, depending on the logic system you are using). Hence:

    1. "if true" is nonsense. There has to be a proposition (x) which is (or is not) true. Hence I'm assuming that you mean "if (x is) true".

    2. "if x is true" means EXACTLY the same as "if x".

    So I cannot see any purpose in what you are attempting to do???

    Postscript:

    If you are talking computer logic, "if true" implies a boolean variable which is either true or false. It is an instruction "if a then b or c" which results in the state b if a is true, or c if not. That's all. So I still do not see what your code is going to achieve???
    Last edited by jedaisoul; 08-28-2014 at 02:24 AM.

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,402
    Still makes no sense as to what you are trying to achieve, perhaps if you have a sample text block and the strings you want to look at and manipulate, maybe it would clarify your goals.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  7. #7
    Join Date
    Mar 2012
    Posts
    1,720
    Perhaps you could have a form with three submit buttons, value "true", "false" and "next". Add an array of three columns: statement, result_true, result_false, e.g.

    "the moon is grey","the moon is made of rock","the moon is made of cheese"
    "all swans are white","there are no black swans","black swans live in Australasia"
    "1+1=3","3","2"
    "3 x 2 = 9","9","6"
    etc.

    The form looks something like:

    <statement> if <true/false> then <result>
    <true> <false> <next>

    The actions are:
    1. A statement is displayed.
    2. a) The user presses "true" in which case "true" is displayed in <true/false> and result_true is displayed. Or...
    2. b) The user presses "false" in which case "false" is displayed in <true/false> and result_false is displayed.
    3. The user presses "next" and the array pointer is incremented and the sequence repeats from 1.

    Now if the purpose of the exercise is to parse input strings, we could modify the above by replacing the "true" and "false" submit buttons by a text input field and an "evaluate" submit button. The form then looks something like:

    <statement> if <input> then <result>
    <evaluate> <next>
    The actions then are:

    1. A statement is displayed.
    2 The user types in the text field and presses "evaluate".
    3. a) If the text input is "true", result_true is displayed. Or...
    3. b) If the text input is "false", result_false is displayed. Or...
    3. c) If neither, "does not compute" is displayed.
    4. The user presses "next" and the array pointer is incremented, and the sequence repeats from 1.

    Is that something like what you intended???
    Last edited by jedaisoul; 08-28-2014 at 04:08 AM.

  8. #8
    Join Date
    Aug 2014
    Posts
    3
    @jedaisoul

    Thank you. Yes it is something like I want. I must to use regular expressions to cut String.

    You know, user writes String to input in this shape: if true/false then "first term" else "second term". And I must choose one of these terms. If is true, result will be "first term". If is false, result will be "second term" and so on.

    I need advice how I can write condition with regular expressions on it.

    Thank you for your time

  9. #9
    This is such a really nice and important information guys.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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