Desktop version

Main > Forum > 1C:Enterprise Platform > 1C:Enterprise – Business applications platform > How to replace the first occurrence of a word in a string?

Forum

Search UsersRules
How to replace the first occurrence of a word in a string?
#1
Active user
Points:: 0
Joined:: Oct 25, 2012

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!

Profile
#2
Active user
Points:: 0
Joined:: Sep 26, 2012

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

Profile
#3
Active user
Points:: 0
Joined:: Oct 25, 2012

Thank you, Ivan! You're superfast!

Profile
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)



© 1C LLC. All rights reserved
1C Company respects the privacy of our customers and visitors
to our Web-site.