Page 1 of 1

how to read a string to extract the content?

Posted: Mon Mar 31, 2014 9:55 am
by beber005
Hey every one,

I'm sorry for this question but I did not success to do that. I have a small problem with this sting (for example) : N/ABFG/P/1011
I want to delete the characters located after the last slah.

If someone has a solution thank's a lot for your attention.

Re: how to read a string to extract the content?

Posted: Mon Mar 31, 2014 10:07 am
by Alan Kirk
beber005 wrote:Hey every one,

I'm sorry for this question but I did not success to do that. I have a small problem with this sting (for example) : N/ABFG/P/1011
I want to delete the characters located after the last slah.

If someone has a solution thank's a lot for your attention.
You would need to find the location of the last slash. One way of doing this is to create a loop to call the Scan function until it returns 0. Then you can simply use the last location that you found in conjunction with the SubSt function to parse out the part before the last slash.

Alternatively you could just get the length of the string using the Long function, then keep using the SubSt function to check each character from the last one backwards until you hit a slash. Then it's just a case of making another call to SubSt to get the characters that appear before the slash. I'm sure that a perusal of the Text Rules functions will give you some further ideas.

However the best one of all may be to fix the data at the source so that it's supplying values without those surplus characters in the first place...

Re: how to read a string to extract the content?

Posted: Mon Mar 31, 2014 11:09 am
by beber005
Thank you very much for your response. I have a question because I thought well the SCAN function, but the problem is that I have 3 / my sentence. What do you think of this code? :

Code: Select all

vOTP1 = Scan('/',vOTP);
While (vOTP1 <> 0);
	vNewOTP = Subst(vOTP1,?,?);

Re: how to read a string to extract the content?

Posted: Mon Mar 31, 2014 11:22 am
by beber005
Rather that

Code: Select all

vOTP1 = SCAN('/',vOTP);
While (vOTP1 <> 0);
	voldOTP1 = NumberToString(vOTP1);
	vNewOTP = SUBST('voldOTP1',vOTP1,vOTP1);
End;
What do you think ?

Re: how to read a string to extract the content?

Posted: Mon Mar 31, 2014 11:48 am
by declanr

Code: Select all

iCheck = 0;
iChar = long ( vOPT);
While ( iCheck = 0 );
      sChar = subst ( vOPT, iChar, 1 );
      If ( sChar @='/' );
            iCheck = 1;
            sTrim = Subst ( vOPT, 1, iChar );
      Else;
            iChar = iChar - 1;
      Endif;
End; 

Re: how to read a string to extract the content?

Posted: Mon Mar 31, 2014 12:43 pm
by beber005
Thank's a lot for your answer !! :)

What do you think about my code ? :

Code: Select all

longueur = LONG(vOTP);
vNewOTP = SUBST(vOTP,1,(longueur-5));
This is the same thing no ?

Re: how to read a string to extract the content?

Posted: Mon Mar 31, 2014 12:55 pm
by declanr
beber005 wrote:Thank's a lot for your answer !! :)

What do you think about my code ? :

Code: Select all

longueur = LONG(vOTP);
vNewOTP = SUBST(vOTP,1,(longueur-5));
This is the same thing no ?
I assumed that the challenge was finding out how many characters to remove but yes if you know its always 4 then your subst code will work.

Just testing it in your model and assessing the result will always be your best test though.

Re: how to read a string to extract the content?

Posted: Mon Mar 31, 2014 1:13 pm
by beber005
After reflexion yes it is always 4. I keep your code and I thank you very much for your help "Declanr".

Re: how to read a string to extract the content?

Posted: Tue Apr 01, 2014 6:01 am
by beber005
I was testing your program and it works but it returns to me, at the end of the expression a slah (/). But I don't want this character and I don't arrive to delete it in your code. I think I have just to put a minus 1 but where ? I don't find the solution. I'm so sorry for this lost of time :oops:

Re: how to read a string to extract the content?

Posted: Tue Apr 01, 2014 7:44 am
by Alan Kirk
beber005 wrote:I was testing your program
Presumably you are referring to Declan's code in the 5th post, as opposed to yours. Which, we have apparently established, is all that you need to do if you do already know how many characters need to be removed in each case. Though again it would (generally) be far more efficient to have them removed from the data source.
beber005 wrote:and it works but it returns to me, at the end of the expression a slah (/).
Yes it does.
beber005 wrote:But I don't want this character and I don't arrive to delete it in your code. I think I have just to put a minus 1 but where ? I don't find the solution.
Seriously? You've read the documentation on SubSt, and you don't get what Declan's code is doing?

Declan's code is counting back from the last character position in the string.

Consequently when sChar hits a \ character, counting down from the full string length, iChar is telling you what the position of that character is in the string.

In your example, with the character numbers shown underneath:

Code: Select all

N/ABFG/P/1011

0000000001111
1234567890123
The last slash is at character number *9*. His code has counted back 13 (the last character in the string, not a slash), 12 (not a slash), 11 (not a slash), 10 (not a slash), 9 (Eureka, it's a slash).

The code

Code: Select all

sTrim = Subst ( vOPT, 1, iChar );
is extracting the first how many characters from the original string?

The first *9* characters, the value of the iChar variable. That will of course include the 9th character which, as we established above, is the slash.

Given all of that, could you perhaps make an educated guess as to where you need to subtract 1 to exclude the slash?
beber005 wrote:I'm so sorry for this lost of time :oops:
The colloquialism that you're looking for is actually "waste of time".

Re: how to read a string to extract the content?

Posted: Tue Apr 01, 2014 11:38 am
by beber005
Sorry for my poor english and for your answer.