Replace a string in parameter with a variable - dominotogo

I think I'm making this harder than what it is, but I can't figure out how to use a variable instead of hard coding the string in the call below. I want to replace "John Doe" with my firstName variable...
var firstName = "John Doe";
db.downloadDocumentsBySelection('SELECT Form="People" & FirstName="John Doe"', function(success, param)
I've tried the following but I get an error: NotesDatabase: httpClient error
var firstName = "John Doe";
db.downloadDocumentsBySelection('SELECT Form="People" & FirstName= "+firstName+"', function(success, param)
I'm not sure if I need to escape the quotes? Any help is appreciated.

This should work:
var firstName = "John Doe";
db.downloadDocumentsBySelection('SELECT Form="People" & FirstName= "'+firstName+'"', function(success, param)
Note the combination of " and ' and after FirstName.

Related

add quotes mark to string

I want to concatenate to a string another which contains the " ' " characters
the string look as wanted But when I create an Object with this string, I'm getting a "\" inside it:
I check in https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-ID285 how to deal with special character
EDIT:
I made a mistake in understanding the source of my problem...
When I try to build a URL with URLQueryItem , there Im getting
print(EventToUrl.name) // print John's Event
let name = URLQueryItem(name: "name", value: EventToUrl.name)
print(name.description) //print name=John's Event
print(name.value) // print Optional("John\'s Event")
deepLink.queryItems = [name]
print(deepLink.url) //print Optional(https://www.example.com/?id%3D-KZXZG2bcuKHNdYNcGTF%26name%3DJohn's%2520Event%26....)
And when i try to send this deep link Im getting an error because of the " ' " characters
I also try Eric Aya's answer here :
How to use special character in NSURL?
but that not working for me:
print(EventToUrl.name) //print "John's Event"
let name = URLQueryItem(name: "name", value: EventToUrl.name.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed))
print(name.description) //Print name=John's%20Event
print(name.value) //Print Optional("John\'s%20Event")
print(deepLink.url) //print
Optional(https://www.example.com/?id%3D-KZXWVbBolmso5DT4p2I%26name%3John's%252520Event%26.......)
Self contained code :
var urlTestComponents = URLComponents()
urlTestComponents.scheme = "https"
urlTestComponents.host = "www.example.com"
urlTestComponents.path = "/"
let nameTest = URLQueryItem(name: "name", value: "John's Event")
urlTestComponents.queryItems = [name]
print(urlTestComponents.url)
The last line prints "https://www.example.com/?name=jeremie's%20Event" and If I try to send that (with whatsApp) the url is cut at the middle because of the " ' "
:

Swift2 - Split String to individual characters

I am trying to split a string into individual characters.
The string I want to split: let lastName = "Kocsis" so that is returns something like: ["K","o","c","s","i","s"]
So far I have tried:
var name = lastName.componentsSeparatedByString("")
This returns the original string
name = lastName.characters.split{$0 == ""}.map(String.init)
This gives me an error: Missing argument for parameter #1 in call. So basically it does't accept "" as an argument.
name = Array(lastName)
This does't work in Swift2
name = Array(arrayLiteral: lastName)
This doesn't do anything.
How should I do this? Is There a simple solution?
Yes, there is a simple solution
let lastName = "Kocsis"
let name = Array(lastName.characters)
The creation of a new array is necessary because characters returns String.CharacterView, not [String]

how to replace a text in a string with other text in scala

i have a following string
var str="ArtShare$u002ETotalArtShares"
i want to replace this "$u002E" with "." so that my string will become
"ArtShare.TotalArtShares" to achieve this i used replace function
var str="ArtShare$u002ETotalArtShares"
var replace=str.replace(".","$u002E")
println("replaced string is "+replace)
but its not working following is printed on console
replaced string is ArtShare$u002ETotalArtShares
please guide me where i am doing wrong Thanks
UPDATE after the answer of user nyavro
i tried this
var str="ArtShare$u002ETotalArtShares"
var replace=str.replace("$u002E", ".")
println("replaced string is "+replace)
but output remains same
but the for testing i have tried this
var str1="ArtShare$u002ETotalArtShares"
var replace1=str1.replace("Total", ".")
println("replaced string is "+replace1)
the following is printed
replaced string is ArtShare$u002E.ArtShares
It means code is working when i replaces Total with "." but its not working when i am giving this part of string $u002E please help me i did not understand why is that happening
Update 2
I tried doing this
var str2="ArtShare$u002ETotalArtShares"
var replace2=str2.replace("u002E", ".")
println("replaced string is "+replace2)
the following is printed
replaced string is ArtShare$.TotalArtShares
after that i guess that $sign is causing the problem its not replacing please help me how can i replace that too
Switch arguments of str.replace:
var replace=str.replace("$u002E", ".")
#JasonLenderman was close:
var replace = str.replaceAll("\\$U002E", ".")
But he mentioned that he wasn't having a problem. I initially saw the same thing, but it appears there's a difference between $u002E and $U002E where the string gets special consideration if the "U" is capitalized. If you capitalize the "U" then the following doesn't work:
var replace = str.replace("\\$U002E", ".")

Grails string replace not working?

I have an object being passed to the controller from a view and it contained double spaces in it that I need to remove. I have gone down the standard path and used the correct functionality to try and remove these double spaces but it doesn’t work. The string did also have a space at the from and then end that I managed to remove with .trim().
Below is what I used to try and Replace all the double spaces:
object = params.objectValue.trim()
object.replaceAll(" ", " ")
This did not work and still had the spaces so I tried this:
newObj = ""
object = params.objectValue.trim()
newObj = object
newObj.toString().replace(" ", " ")
here is an example of the string text:
"the person drove the car on 21/12/04 at 12:00"
This didn’t work either, has anyone got any idea as i dont really understand what i can do to remove these double spaces?
Thanks in advance
replaceAll() doesn't mutate the original string, it returns a new string with the replaced elements. You need to reassign params.objectValue to the result of replaceAll()
params.objectValue = params.objectValue.replaceAll(" ", " ").trim()

C# ComboBox Selected Text To Multiple Strings

I have a combobox that has the entries of a full name, eg: John Smith, Mark Tall, etc.
I have written the following:
string FullName = StudentSelectStudentComboBox.Text;
This gets "John Smith" as the string 'FullName'. Is it possible to break down 'FullName' string into 2 strings; FirstName and LastName?
You can just use string.split
string str = "John Adams";
string[] names= str.Split(' '); //names[0]="John" names[1]="Adams"
This answer is similar to the one above but with a twist:
If you want to get fancy you can try:
///////////////
//Names[0] = first name, Name1 = last name
string[] Names = StudentSelectStudentComboBox.Text.Split(' ');
////////

Resources