I have a string "['type':'MultiPolygon', 'coordinates':[[73.31, 37.46], [74.92, 37.24]]]"
How can I replace all single quotes with double quotes and double to with single?
The result should be like this:
'["type":"MultiPolygon", "coordinates":[[73.31, 37.46], [74.92, 37.24]]]'
From link given by #yate, you can find a method:
tr(String sourceSet, String replacementSet)
and apply that to your string as:
def yourString = ...
def changedString = yourString.tr(/"'/,/'"/)
that will do the job.
You want to use the replaceAll method. Since the first conversion will be overridden by the second, you may need a temporary variable:
String replacePlaceholder = '%%%' // Some unlikely-to-occur value
yourString = yourString.replaceAll('\'', replacePlaceholder)
.replaceAll('"', '\'')
.replaceAll(replacePlaceholder, '"')
It's certainly not the most efficient way to do it, but it's a start.
Related
I'm trying to create a raw string that contains three quotes in itself.
The resulting string x should contain something like """abc""".
I've been able to create the string with the following code, but was wondering if there's a simpler solution for this.
val x = """${'"'.toString().repeat(3)}abc${'"'.toString().repeat(3)}"""
There's no easy way to use a triple quote directly in a string literal.
One workaround I've sometimes used is to make an interim variable to hold the triple-quote string.
val quotes = "\"\"\""
val result = "${quotes}abc${quotes}"
I think a simpler way would be to escape them manually, so like:
val x = "\"\"\"abc\"\"\""
My program is something like that:
func = do
text <- getLine
return text
If I read line \123\456, the result is, naturally, \\123\\456.
How can I obtain \123\456 as the result?
Based on the discussion in comments, it looks like you want to parse the string as if it was a string literal, except that it is not surrounded by quotes.
We can make use of of read :: Read a => String -> a here that for a string parses it as if it was a string literal to a string. The only problem is that this string literal is surrounded by double quotes (").
We can thus add these quotes, and work with:
read ('"' : text ++ "\"") :: String
Not every string text is however per se a valid string literal, so the above might fail. For example if the text contains a double quote itself, that is not directly preceded by a backslash (\).
I have a matrix of strings such as the following:
readFiles = [
"11221", "09";
"11222", "13";
"12821", "06";
"13521", "02";
"13522", "13";
"13711", "05";
"13921", "01";
"14521", ".001";
"15712", ".003"
];
These are used to access to some folders and files in an automatic way. Then what I want to do is the following (with ii being some integer):
FileName = strcat('../../Datasets/hc-1/d',readFiles(ii,1),'/d',...
readFiles(ii,1),readFiles(ii,2),'.dat');
data(ii,:) = LoadBinary(FileName, 6);
The string FileName is then generated using double quotes (I'm not sure why). So its value is:
FileName =
"../../Datasets/hc-1/d13921/d1392101.dat"
The function LoadBinary() returns an error when trying to perform the following operation:
lastdot = strfind(FileName,'.');
FileBase = FileName(1:lastdot(end)-1); % This line
However, if I create the string FileName manually using single quotes, the function works okay.
In a nutshell, if I try to index a string (FileName(1:lastdot(end)-1)) that is created with the lines above (leading to FileName = "../../Datasets/hc-1/d13921/d1392101.dat"), MATLAB returns an error. If I create it manually with single quotes (FileName = '../../Datasets/hc-1/d13921/d1392101.dat'), the function works right.
Why does this happen? Is there a way to fix it (i.e. convert the double-quoted string into a single-quoted one)?
Double quotes are String array, while Single one are Char array. You can convert your string array to a char one using the function char.
So you'd write :
CharFileName = char(FileName)
And it should resolve your issue.
I am trying to include a single double quote in a string during a concatenation within JuliaLang, as below:
tmpStr = string(tmpStr, string("graph [label=\" hi \"]; "))
The output in the text file written with writedlm is:
graph [label="" hi ""]
How can I modify the string interpolation to include only a single double quote instead of this repetition?
The extra double quotes come from writedlm. writedlm uses standard CSV escaping method, which surrounds special characters with double quotes, and uses "" to represent a single double quote. This is OK, as long as you do the inverse transformation when reading the file.
A good method to trace such problems is to create a minimal working example. In this case, something like:
writedlm("tst.tst",["\""])
Which writes tst.tst, but tst.tst now has:
""""
But when read properly:
julia> data = readdlm("tst.tst")
1×1 Array{Any,2}:
"\""
As expected.
Another option to avoid getting the extra quotes is to add quotes=false as an option to writedlm, as in the following example:
julia> writedlm(STDOUT,["\""],quotes=false)
"
Here is my problem
String text = "I am here"
resutl = ofy().load().type(M.class).filter("good = "+text).first().now();
Since text contains space, how do I pass it to Objectify? Do I place it in single quotes? double quotes? what?
The filter() method takes two parameters:
ofy().load().type(M.class).filter("good =", text).first().now();
There is no need to escape the value.
ofy().load().type(M.class).filter("good",text).first().now()
There is no need for "=". It is a default operator.