Fungsi Ganti VBScript


Referensi VBScript Lengkap

Fungsi Replace menggantikan bagian tertentu dari string dengan string lain beberapa kali tertentu.

Sintaksis

Replace(string,find,replacewith[,start[,count[,compare]]])

Parameter Description
string Required. The string to be searched
find Required. The part of the string that will be replaced
replacewith Required. The replacement substring
start Optional. Specifies the start position. Default is 1. All characters before the start position will be removed.
count Optional. Specifies the number of substitutions to perform.
Default value is -1, which means make all possible substitutions
compare Optional. Specifies the string comparison to use. Default is 0

Can have one of the following values:

  • 0 = vbBinaryCompare - Perform a binary comparison
  • 1 = vbTextCompare - Perform a textual comparison

Contoh

Contoh 1

Ganti kata "indah" dengan "fantastis":

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"beautiful","fantastic"))

%>

Output dari kode di atas akan menjadi:

This is a fantastic day!

Contoh 2

Ganti huruf "i" dengan "##":

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"i","##"))

%>

Output dari kode di atas akan menjadi:

Th##s ##s a beaut##ful day!

Contoh 3

Ganti huruf "i" dengan "##", mulai dari posisi 15:

Perhatikan bahwa semua karakter sebelum posisi 15 dihapus.

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",15))

%>

Output dari kode di atas akan menjadi:

t##ful day!

Contoh 4

Ganti 2 kemunculan pertama huruf "i" dengan "##", mulai dari posisi 1:

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",1,2))

%>

Output dari kode di atas akan menjadi:

Th##s ##s a beautiful day!

Contoh 5

Ganti huruf "t" dengan "##", dengan tekstual, dan biner, perbandingan:

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"t","##",1,-1,1) & "<br />")
response.write(Replace(txt,"t","##",1,-1,0))

%>

Output dari kode di atas akan menjadi:

##his is a beau##iful day!
This is a beau##iful day!

Referensi VBScript Lengkap