Guidance
指路人
g.yi.org
Guidance Forums / Reginald Rexx / Subtracting a string from another string

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Subtracting a string from another string
#5135
Posted by: 2004-09-22 23:16:36
I need to "subtract" one text string from another. I'll just give an example:
string1 = 'C:\HRGLAP\Archive\2003\ELK\Budget\spreadsheet.xls'
string2 = 'C:\HRGLAP\Archive\'
The desired result should be a variable containing:
'2003\Budget\spreadsheet.xls'
string2 (and the corresponding portion of string1) can vary in both content and length. string1 will always begin with the contents of string2, but what comes after will vary in content, length, and number of additional subdirectories.

Before I reinvent the wheel, I thought I would ask here if anyone has done anything like this (or even has a suggestion on how to approach the problem).
Message2. Re: Text string manuipulation
#5136
Posted by: kjactive 2004-09-23 00:23:46
There are several ways to do this.

If you already know at what position string2 appears within string1 (which you do above -- it appears starting at the first character), then you can use the DELSTR function:
string1 = DELSTR(string1, LENGTH(string2)+1)
The above instruction should return the remaining part of string1, and reassign it to the string1 variable. (If you don't want to alter the original value of string1, then assign DELSTR's return value to a different variable).

Since the position happens to be at the very first character (ie, the rightmost character), you could also use the RIGHT function to do the same thing:
string1 = RIGHT(string1, LENGTH(string1) - LENGTH(string2))
If you don't know where string2 appears within string1, then you could use the POS function to find out where it starts, and then use that position with DELSTR:
position = POS(string2, string1)
IF position \= 0 THEN
   string1 = DELSTR(string1, position, LENGTH(string2))
If you know that string1 appears once and only once within string1, then you can replace the above two instructions with:
string1 = CHANGESTR(string2, string1, "")
But note that if string2 appears more than once, then all of those occurences will be removed with CHANGESTR. (You can use COUNTSTR to check if string2 appears more than once).

Thats it I hope.

Rexx is a great language for string manipulation.

K?re

P.S. One strange thing with our example is that ELK disapeared. Why?
Message3.
#11484
Posted by: Jeff Glatt 2007-08-14 06:32:19
If you want to trim off a substring from the start of another string, but you're not sure if that substring does in fact appear at the start of the string, then the ABBREV() built-in is most helpful. ABBREV will test if a string begins with a particular substring. Then you can use the RIGHT() built-in to hack off the substring.
string1 = 'C:\HRGLAP\Archive\2003\ELK\Budget\spreadsheet.xls'
string2 = 'C:\HRGLAP\Archive\'
IF ABBREV(string1, string2) THEN string1 = RIGHT(string1, LENGTH(string1) - LENGTH(string2))
Note that because you're dealing with a Windows path, then you may want to set the CASELESS OPTION before ABBREV.
Message4.
#11580
Posted by: Geoffrey 2007-08-19 16:32:08 Last edited by: Geoffrey 2007-08-19 21:52:54 (Total edited 1 time)
How about

string1 = 'C:\HRGLAP\Archive\2003\ELK\Budget\spreadsheet.xls'
string2 = 'C:\HRGLAP\Archive\'

parse var string1  aa (string2) bb

say bb

plus any error checking necessary

Geoffrey
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Sat 2022-10-1  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0