Topic
  • 13 replies
  • Latest Post - ‏2019-05-24T18:19:17Z by llandale
Christopher Cote
Christopher Cote
67 Posts

Pinned topic Retrieving a substring up to last index of a character

‏2016-10-31T15:32:25Z |

I am currently retrieving the Object Number from a DOORS script, but I need to get just a portion of it.  I want to get a substring from the first character to the last index of the hyphen.  Since I don't know the exact positions of the hyphens or how many there may be, how do I retrieve everything from the first character to the position of the last hyphen?  I've seen that in order to get a substring, you would use strVar[firstCharPos, lastCharPos], but how would I determine the lastCharPos value?  I've been trying to use the JavaScript functions of substring, indexOf and lastIndexOf, but these are apparently not the proper way to get the substrings.  What is the proper way?

 

Chris

  • DOORSHAM
    DOORSHAM
    189 Posts
    ACCEPTED ANSWER

    Re: Retrieving a substring up to last index of a character

    ‏2016-11-01T16:58:50Z  

    Thank you folks.  I took a look at the "matches" function description in the reference manual.  I can understand how this would give you whether or not there is a hyphen in the string.  It looks like the "match" function would be more like what I want.  However, how would I get the last match if there are 2 or more hyphens in the string.  For the most part, the object_number will have only 1 hyphen, but there are a few that are similar to "0-1.0-1.0-4.0-2" and I would want to get "0-1.0-1.0-4.0".

     

    Chris Cote

    //EZmethod requiring no thought

    /*Prints 0-1.0-1.0-4.0  */
    string s = "0-1.0-1.0-4.0-2"

    int LASTDASH(string s)
    { int l = length s , j, i=0
      for (j=0;j<l; j++){ if(s[j]=='-') i=j}
      return i
    }

    int i = LASTDASH(s )
    print s[0:i-1]

  • DOORSHAM
    DOORSHAM
    189 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2016-10-31T15:49:44Z  

    Look at matches.

  • EHcnck
    EHcnck
    213 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2016-10-31T15:51:24Z  
    string section(Object o) {
            /*
                    match - returns word style section numbers
            */
            Regexp sectionNum = regexp2 "([0-9]+(\\.[1-9])*)+"
            string objNum = number(o)
            if ( sectionNum objNum ) {
                    objNum = objNum[match 0]
            }
            delete sectionNum
            return objNum
    } // section
    

     

  • Christopher Cote
    Christopher Cote
    67 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2016-11-01T14:34:11Z  

    Thank you folks.  I took a look at the "matches" function description in the reference manual.  I can understand how this would give you whether or not there is a hyphen in the string.  It looks like the "match" function would be more like what I want.  However, how would I get the last match if there are 2 or more hyphens in the string.  For the most part, the object_number will have only 1 hyphen, but there are a few that are similar to "0-1.0-1.0-4.0-2" and I would want to get "0-1.0-1.0-4.0".

     

    Chris Cote

  • DOORSHAM
    DOORSHAM
    189 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2016-11-01T16:58:50Z  

    Thank you folks.  I took a look at the "matches" function description in the reference manual.  I can understand how this would give you whether or not there is a hyphen in the string.  It looks like the "match" function would be more like what I want.  However, how would I get the last match if there are 2 or more hyphens in the string.  For the most part, the object_number will have only 1 hyphen, but there are a few that are similar to "0-1.0-1.0-4.0-2" and I would want to get "0-1.0-1.0-4.0".

     

    Chris Cote

    //EZmethod requiring no thought

    /*Prints 0-1.0-1.0-4.0  */
    string s = "0-1.0-1.0-4.0-2"

    int LASTDASH(string s)
    { int l = length s , j, i=0
      for (j=0;j<l; j++){ if(s[j]=='-') i=j}
      return i
    }

    int i = LASTDASH(s )
    print s[0:i-1]

  • Mathias Mamsch
    Mathias Mamsch
    2649 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2016-11-07T08:26:12Z  
    • DOORSHAM
    • ‏2016-11-01T16:58:50Z

    //EZmethod requiring no thought

    /*Prints 0-1.0-1.0-4.0  */
    string s = "0-1.0-1.0-4.0-2"

    int LASTDASH(string s)
    { int l = length s , j, i=0
      for (j=0;j<l; j++){ if(s[j]=='-') i=j}
      return i
    }

    int i = LASTDASH(s )
    print s[0:i-1]

    By the way, I just remebered that the findPlainText function has a "reverse" parameter which allows searching from the end of the string. So for completeness sake I would add the solution: 

    string s = "Hallo - Du"
    
    int index = 0, dummy = 0
    if (findPlainText(s, "-", index, dummy /* length */, true, true /* reverse */)) {
            print "Found at index " index "\n"; 
    } else {
        print "Not found.\n"
    }
    

    Regards, Mathias

  • DOORSHAM
    DOORSHAM
    189 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2016-11-07T11:43:13Z  

    By the way, I just remebered that the findPlainText function has a "reverse" parameter which allows searching from the end of the string. So for completeness sake I would add the solution: 

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">string s = "Hallo - Du" int index = 0, dummy = 0 if (findPlainText(s, "-", index, dummy /* length */, true, true /* reverse */)) { print "Found at index " index "\n"; } else { print "Not found.\n" } </pre>

    Regards, Mathias

    Only problem is findPlainText is not documented -- is it wise to tell users to use undocumented perms.

     

  • Mathias Mamsch
    Mathias Mamsch
    2649 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2016-11-07T14:49:55Z  
    • DOORSHAM
    • ‏2016-11-07T11:43:13Z

    Only problem is findPlainText is not documented -- is it wise to tell users to use undocumented perms.

     

    It is documented, I think at least since DOORS 9.4 ? What DOORS Version Help are you using? 

  • Christopher Cote
    Christopher Cote
    67 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2019-05-20T19:24:17Z  
    • DOORSHAM
    • ‏2016-11-01T16:58:50Z

    //EZmethod requiring no thought

    /*Prints 0-1.0-1.0-4.0  */
    string s = "0-1.0-1.0-4.0-2"

    int LASTDASH(string s)
    { int l = length s , j, i=0
      for (j=0;j<l; j++){ if(s[j]=='-') i=j}
      return i
    }

    int i = LASTDASH(s )
    print s[0:i-1]

    I've just thought of a slightly better solution to my problem:

    string s = "0-1.0-1.0-4.0-2"
    
    int LASTDASH(string s) {
        int l = length s, j, i=0;
        for (j=l; j>0; j--) {
            if (s[j] == '-') {
                i = j;
                break;
            }
        }
        return i
    }
    
    int i = LASTDASH(s);
    print s[0:i-1];
    

    This method is slightly different in that it starts at the last character and moves backward until it finds a hyphen.  When it does, it sets the value of i and breaks out of the loop.  Granted, this only saves a few microseconds, but if I had a really long string, it will be able to find it faster.

     

    Chris

  • Mike.Scharnow
    Mike.Scharnow
    585 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2019-05-20T22:22:21Z  

    I've just thought of a slightly better solution to my problem:

    <pre class="javascript dw" dir="ltr">string s = "0-1.0-1.0-4.0-2" int LASTDASH(string s) { int l = length s, j, i=0; for (j=l; j>0; j--) { if (s[j] == '-') { i = j; break; } } return i } int i = LASTDASH(s); print s[0:i-1]; </pre>

    This method is slightly different in that it starts at the last character and moves backward until it finds a hyphen.  When it does, it sets the value of i and breaks out of the loop.  Granted, this only saves a few microseconds, but if I had a really long string, it will be able to find it faster.

     

    Chris

    If you like regular expressions, why not this way?

     

    // collect everything from the beginning in group #1. Then there must be a hyphen, then there may be more characters which are not a hyphen, up to the end of the string
    Regexp upToLastDash = regexp2 "^(.+)\\-[^\\-]*$"
    string checkem = "0.3-3.4-4-5555"
    if (upToLastDash checkem) {
            print checkem[match 1]
    }
    

     

  • llandale
    llandale
    3083 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2019-05-23T00:21:24Z  
    • EHcnck
    • ‏2016-10-31T15:51:24Z
    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">string section(Object o) { /* match - returns word style section numbers */ Regexp sectionNum = regexp2 "([0-9]+(\\.[1-9])*)+" string objNum = number(o) if ( sectionNum objNum ) { objNum = objNum[match 0] } delete sectionNum return objNum } // section </pre>

     

    If I have a function that uses a static RegExp, I just define it above the function.  They you don't waste time creating/deleting the RegExp.

  • llandale
    llandale
    3083 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2019-05-23T00:25:43Z  

    I've just thought of a slightly better solution to my problem:

    <pre class="javascript dw" dir="ltr">string s = "0-1.0-1.0-4.0-2" int LASTDASH(string s) { int l = length s, j, i=0; for (j=l; j>0; j--) { if (s[j] == '-') { i = j; break; } } return i } int i = LASTDASH(s); print s[0:i-1]; </pre>

    This method is slightly different in that it starts at the last character and moves backward until it finds a hyphen.  When it does, it sets the value of i and breaks out of the loop.  Granted, this only saves a few microseconds, but if I had a really long string, it will be able to find it faster.

     

    Chris

    I think you want this:

        for (j=l-1; j>=0; j--) {

  • Christopher Cote
    Christopher Cote
    67 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2019-05-23T12:42:35Z  
    • llandale
    • ‏2019-05-23T00:25:43Z

    I think you want this:

        for (j=l-1; j>=0; j--) {

    To answer your question Lou, I'm not very good with creating regular expressions, so I tend to steer away from them.  Perhaps when I get better with them, I'll use them more often than looping through the string.  As for your last post, I probably should loop all the way to the first character (>=0 vs. >0), but I'm making an assumption that the first character in the string is never a hyphen.

     

    Chris

  • llandale
    llandale
    3083 Posts

    Re: Retrieving a substring up to last index of a character

    ‏2019-05-24T18:19:17Z  

    To answer your question Lou, I'm not very good with creating regular expressions, so I tend to steer away from them.  Perhaps when I get better with them, I'll use them more often than looping through the string.  As for your last post, I probably should loop all the way to the first character (>=0 vs. >0), but I'm making an assumption that the first character in the string is never a hyphen.

     

    Chris

    I'm terrible with Regular Expressions.  I have to spell everthing out and comment all over it, in order for me to make it work.

    Here's an example.  Notice that setting the string and printing it before the "regexp2" command helps tremendously in debugging it.

    string  plc_sRE_Script_FileParts        =
    
                    "^"                                   //              start of string                 -
                    "(" lc_re_strAnyChar "*)"           // match 1: Base File Name before Extension             -
                    plc_sRE_Script_Extention                // match 2: period, then valid Script extension         -
                    "(" lc_re_strAnyChar "*)"           // match 3: Trailing chars that have no Period (and so no Extension)    -
                    "$"                                   //              end of string                   
                                    // EOLs are allowed but make no sense to do so.
                                    // "Greedy" matching.
    //print "plc_sRE_Script_FileParts = [" plc_sRE_Script_FileParts "]\n"
    Regexp  plc_re_Script_FileParts = regexp2(plc_sRE_Script_FileParts)