Вопрос

Does anybody know how to make function in Libreoffice basic like str_ireplace in PHP?
I want to use in my cell function.

str_ireplace(search - range of cells, replace - range of cells, text)

or at least str_replace

Это было полезно?

Решение

I made really simple function

Function Str_ireplace(Search As Variant, Replace As Variant, Source As String)

  Dim Result As String
  Dim StartPos As Long
  Dim CurrentPos As Long
  Dim CurrentSearch As String
  Dim CurrentReplace As String

  Result = ""

  For Row = Lbound( Search, 1 ) To Ubound( Search, 1 )
    For Col = LBound(Search, 2) To UBound(Search, 2)
        StartPos = 1
        CurrentPos = 1
        CurrentSearch = Search(Row, Col)
        CurrentReplace = Replace(Row, Col)
        Result = ""


        Do While CurrentPos <> 0
            CurrentPos = InStr(StartPos, Source, CurrentSearch)
            If CurrentPos <> 0 Then
                Result = Result + Mid(Source, StartPos, _
                CurrentPos - StartPos)
                Result = Result + CurrentReplace
                StartPos = CurrentPos + Len(CurrentSearch)
            Else
                Result = Result + Mid(Source, StartPos, Len(Source))
            End If                ' Position <> 0
        Loop
        Source = Result 
    Next 
  Next


  Str_ireplace = Result    
End Function

I used this as example: http://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Strings_(Runtime_Library)

Другие советы

try this

Syntax

REPLACE("Text"; Position; Length; "NewText")

Text refers to text of which a part will be replaced.

Position refers to the position within the text where the replacement will begin.

Length is the number of characters in Text to be replaced.

NewText refers to the text which replaces Text.

Example

=REPLACE("1234567";1;1;"444") returns "444234567". One character at position 1 is replaced by the complete NewText.

https://help.libreoffice.org/Calc/Text_Functions#Example_13

or

SUBSTITUTE("Text"; "SearchText"; "NewText"; Occurrence)

Example

=SUBSTITUTE("123123123";"3";"abc") returns 12abc12abc12abc.

=SUBSTITUTE("123123123";"3";"abc";2) returns 12312abc123.

https://help.libreoffice.org/Calc/Text_Functions#SUBSTITUTE

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top