replace string via builtin function

+1 vote
asked Jan 25, 2025 in Wanted features by kirchsth (7,760 points)
I would like to replace the first x. findings in a string with another value like
     !$replaced = %replacestr("ABXYABAB", "AB", "abc", 2)   
     ' $replaced should be "abcXYabcAB"

but I didn't found it in the documentation.
Did I oversee it?

BR
Helmut
commented Mar 16, 2025 by Daniel Mason

Hello, welcome to the forums!
Efficient data processing is essential in both programming and academic evaluations. Just as built-in string functions like Python's str.replace(), JavaScript's replace(), or Java's replaceAll() streamline text modifications, a reliable CGPA to Percentage Calculator simplifies grade conversion. By automating the process with predefined formulas, it ensures accuracy and consistency, helping students and professionals interpret their grades effortlessly. Read more

1 Answer

0 votes
answered Mar 5, 2025 by dickmaley (4,160 points)

The plantuml documentation for built-in functions can be found at https://plantuml.com/preprocessing

There is no reference to a built-in function %replacestr.

The closest fit would be a combination of %substr, %strpos and %strlen.

I was able to create the following 

image

with

@startuml
!function pos(substring,wholeString)
!$result = 0
!if (%strlen(wholeString) == 0)
!  return $result
!endif

!if (%strlen(substring) == 0)
!  return $result
!endif
!$result=%strpos(wholeString,substring)+1
!return $result
!endfunction

!function copy(source,startingindex, count);
!$result = ""
!$index=startingindex
!if (startingindex < 1)
  !startingindex=1
!endif
!if (count>%strlen(source))
  !count=%strlen(source)
!endif
!if (%strlen(source) == 0)
  !return $result
!endif
!$result=%substr(source,startingindex - 1,count)
!return $result
!endfunction
!function minusOne(n)
!$result = n - 1
!return $result
!endfunction

!function stringReplaceOnce(sourceString, oldPattern, newPattern)
!if (%strlen(sourceString) == 0)
!  return sourceString
!endif
!if (%strlen(oldPattern) == 0)
!  return sourceString
!endif
!if (pos(oldPattern,sourceString) == 0)
!  return sourceString 
!endif
!return copy(sourceString, 1, pos(oldPattern,sourceString) - 1)+ newPattern + copy(sourceString,pos(oldPattern,sourceString) + %strlen(oldPattern),%strlen(sourceString) - (pos(oldPattern,sourceString) + %strlen(oldPattern)) + 1)
!endfunction

!function replacestr(sourceString, oldPattern, newPattern)
!if (%strlen(sourceString) == 0)
!  return sourceString
!endif
!if (%strlen(oldPattern) == 0)
!  return sourceString
!endif
!if (pos(oldPattern,sourceString) == 0)
!  return sourceString 
!endif
!$result=sourceString
!$index=0
!while $index<50
!if (pos(oldPattern,$result) == 0)
!return $result
!else
!$result=stringReplaceOnce($result, oldPattern, newPattern)
!endif
!$index=$index+1
!endwhile

!return $result
!endfunction

note as Results
    "replacestr" of ("ABXYABAB", "AB", "abc") produces : replacestr("ABXYABAB", "AB", "abc")
    correct answer is                                                        : abcXYabcabc
end note
@enduml

Dick Maley

commented Mar 6, 2025 by kirchsth
Thank you for your implementation. I did something similar in the meantime.
But I think your source is a good sample which can easily reused by other.

BR Helmut
...