Hi! I was wondering the way to do something like this (VB.net):
dim s1 as string = “Hello”
dim s2 as string = “World”
dim s3 as string = String.Format("{0} {1} my friends!", s1, s2)
The output in s3 will be “Hello World my friends!” because String.Format build the string replacing the {0}, {1}… with the variables written after that.
Is there a way to do it in Xojo? By the way, I’m using Xojo Web.
Thank you very much!!
No there is no similar function provided in XOJO.
However you could write an extension method for string that would do the same thing. Alternatively go old school and just use a combination of concatenation and replace or replace all to do the trick.
The simplest way would be:
dim s1 as string = "Hello"
dim s2 as string = "World"
dim s3 as string = s1 + " " + s2 + " my friends!"
There’s not something builtin, but I think you could code it yourself. Extend String, and use paramarray to grab the strings. After that use regex or string processing to replace {x} with the string that should be there.
dim s1 as string = "Hello"
dim s2 as string = "World"
dim s3 as string = s1 + " " + s2 + " my friends!"
Edit: Oops, Albin beat me to it.
Yes, I know that the “easy” way is the one that Albin Kiland gave, but it complicates a lot if the amount of variables that I want to use is high.
I already thought to code it myself using paramarray as you said, but I wanted to ask first.
Thank you very much everybody!
When you do code it yourself, be careful about how you handle the tokens. For example, if a token takes the form “{0}” as in your example, suppose you had a string like this:
“This {0} rocks the {1}”
And you gave it the parameters ("{1}", “pershnizzle”), you don’t want to end up with the string “This pershnizzle rocks the pershnizzle”.
I’ve got old some source code for this called TemplateString. Let me dig it out in the evening.
It turns out I wrote this already for my M_String module. I just updated the code this morning so this is the new version. It takes tokens either in the form “${0}” or “\?”. The former lets you repeat and be specific, the latter just replaces in order.
Function Expand_MTC(src As String, ParamArray vars() As Variant) As String
// Takes a string and replaces the occurrences of a token with the values of the
// Variants. Those Variants must be able to get a string value.
// Values that exceed the number of tokens will be ignored.
//
// Two forms of tokens are accepted. If "\\?" is present in the string, it will
// replace those in order. Otherwise, it will look for the form "${#}"
// where # represents a specific value in the array.
//
// Examples:
// s = Expand_MTC( "The ${1} beat the ${2} by a score of ${3} to ${4}", "Yankees", "Red Sox", 15, 1 )
// s will be "The Yankees beat the Red Sox by a score of 15 to 1"
// s = Expand_MTC( "\\? is a great \\?", "Derek Jeter", "shortstop" )
// s will be "Derek Jeter is a great shortstop"
if vars.Ubound = -1 then return src
if src = "" then return src
dim enc as TextEncoding = src.Encoding
const kToken = "\\?"
if src.InStr( kToken ) = 0 then
// Using the "${#}" form
for i as Integer = 0 to vars.Ubound
dim token as string = "${" + str( i + 1 ) + "}"
src = src.ReplaceAll( token, vars( i ).StringValue )
next i
else // Using the "\\?" form
dim srcArr() as string = src.Split( kToken )
dim resultArr() as string
if srcArr.Ubound <> -1 then
resultArr.Append srcArr( 0 )
dim srcIndex as Integer = 1
dim varIndex as Integer = 0
do until srcIndex > srcArr.Ubound or varIndex > vars.Ubound
dim thisVar as String = vars( varIndex ).StringValue.ConvertEncoding( enc )
varIndex = varIndex + 1
resultArr.Append thisVar
resultArr.Append srcArr( srcIndex )
srcIndex = srcIndex + 1
loop
// Add any missing source
dim insertToken as string = kToken.ConvertEncoding( enc )
for i as Integer = srcIndex to srcArr.Ubound
resultArr.Append insertToken
resultArr.Append srcArr( i )
next i
src = join( resultArr, "" )
end if
end if
if src.Encoding <> enc then
src = src.ConvertEncoding( enc )
end if
return src
End Function
OMG! You are awesome! I was just starting to do it and now I find that you’ve already done it!
Thank you very much, I will take a look on it!
Really? I’ve been censored “o m g”. ¬¬