How to replace the first occurrence of a word in a string?

The 1C:Enterprise developers forum

#1
People who like this:0Yes/0No
Active user
Rating: 2
Joined: Oct 25, 2012
Company: Care Small Animal Hospital

Aho!

I'm doing the inventory review and I need to remove a substring "cage" from about 1000 of strings. Only the first time this string occurs, case insensitive and only if it occurs at the beginning, say, not farther than 50 characters from the beginning of the source string. Can anyone please help me with the implementation? I tried to use StrReplace, but it replaces all words. However, in some cases, the "cage" substring is a part of other words or is a useful part of a description and should stay unchanged.

Thanks in advance!

 
#2
People who like this:0Yes/0No
Active user
Rating: 7
Joined: Sep 26, 2012
Company: individual

If I right understand your task:

Code
Function RemoveCage(str)
   removingSubString = "cage";
   lenSub = StrLen(removingSubString);
   
   firts50 = Lower(Mid(str, 1, 50)); // use Lower for case insensitive
   pos = Find(firts50, removingSubString);
   If (pos > 0) Then
      return Left(str, pos - 1) + Mid(str, pos+lenSub); // remove cage
   else
      return str; // nothing remove
   EndIf
EndFunction

Edited: ivan avdonin - Dec 07, 2012 12:42 PM
 
#3
People who like this:0Yes/0No
Active user
Rating: 2
Joined: Oct 25, 2012
Company: Care Small Animal Hospital

Thank you, Ivan! You're superfast!

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)
Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.