Grails string replace not working? - string

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()

Related

How do I take a string and turn it into a list using SCALA?

I am brand new to Scala and having a tough time figuring this out.
I have a string like this:
a = "The dog crossed the street"
I want to create a list that looks like below:
a = List("The","dog","crossed","the","street")
I tried doing this using .split(" ") and then returning that, but it seems to do nothing and returns the same string. Could anyone help me out here?
It's safer to split() on one-or-more whitespace characters, just in case there are any tabs or adjacent spaces in the mix.
split() returns an Array so if you want a List you'll need to convert it.
"The dog\tcrossed\nthe street".split("\\s+").toList
//res0: List[String] = List(The, dog, crossed, the, street)

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", ".")

Is there a way to add quotes to a multi paragraph string

I wrote the following line:
string QuoteTest2 = "Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
This example went well, but what can I do in case I want to write a multi paragraph string including quotes?
The following example shows that puting '#' before the doesn't cut it..
string QuoteTest2 = #"Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
The string ends and the second quote and the over just gives me errors, what should I do?
Use double quotes to escape ""
e.g.
string QuoteTest2 = #"Benjamin Netnayahu,""BB"", said that: ""Israel will not fall""";

Alternative to CONTAINS method?

Giving the string below with "server type" separated by comma:
string serverTypeList = "DB, IIS, CMDB";
//server.Type in the loop below should have value of "MDB"
My problem is that in this scenario it will return TRUE because "MDB" string is inside the serverTypeList.
I need it to return TRUE only if it matches a type of "MDB" and not "CMDB":
...
from site in SiteManager.Sites
from server in site.Servers
where
serverTypeList.Contains(server.Type)
select new Server()
{ ID=server.ID, SiteName=site.Name }
...
How can I change the code above?
Thank you
(", " + serverTypeList + ", ").Contains(", " + server.Type + ", ")
is one standard way to handle this. I'm not clear on the language you're using, so I don't know the exact syntax you would need, but the general idea is to ensure that the term appears between delimiters by forcing delimiters before and after the list string.

Path.Combine with spaces in passed parameter?

string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
string exeDir = Path.GetDirectoryName(exeFile);
string fileName = Path.Combine(exeDir, #"..\..\xml\SalesOrderXMLData.csv.xml");
Hello,
The above code works if the project is in, for example,
C:\Code\
but not if its in
C:\Documents and Settings\Naim\My Documents..
If i have the string, i would use escape characters where needed, but in this case, i dont know how to get around this.
Update: result fileName = "D:\Naim\My%20Documents\Visual%20Studio%202008\Projects\XML_Gen\XML_Gen\bin\Debug\..\..\xml\SalesOrderXMLData.csv.xml"
Any help appreciated.
Thanks
It's probably the URI. Use Assembly.GetEntryAssembly().Location, and pass it directly to Path.GetDirectoryName().
code below works, though I don't know why the above wasn't working. Changed AbsolutePath to LocalPath
string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).LocalPath;
string exeDir = Path.GetDirectoryName(exeFile);
string fileName = Path.Combine(exeDir, #"..\..\xml\SalesOrderXMLData.csv.xml");

Resources