MATLAB line continuation within string - string

In MATLAB, ... is used to continue a line to the next line. But if I want to continue a long string within quotation, what can I do? ... will be treated as a part of the string itself.
Using [] is not a perfect solution since in most cases I use sprintf/fprintf to parse a long string like sql query. Using [] would be cumbersome. thanks.

If you put the string in brackets, you can build it in several pieces:
s = ['abc' 'def' ...
'ghi'];
You can then split that statement into several lines between the strings.

answer=['You can divide strings '...
,'by adding a comma '...
,'(as you probably know one year later).'];

You can use strcat or horzcat, which gives you somewhat more options than [], including the ability to mix in variables along with the hardcoded values.

Related

How to remove white spaces and capitalize every first letter in the string in the robotramework

How can I remove white spaces and capitalize every first letter in the string in the robotframework, so later I use the result in the Selenium library calls?
Test to Unlock the Service Account:
Open Browser ${URL} ${Browser}
${string_1} = get text ${question_1}
${temp_answer} = set variable ${string_1}.title()
${answer}= evaluate ${string_1}.replace(" ","")
Input Text ${Answer_1} ${answer}
sleep 5s
Input:
Legal business name
Output:
LegalBusinessName?
You were close to achieving it, but made two crucial mistakes. The first one is you used Set Variable and tried calling the python's title() string method in the argument - but that doesn't work for the keyword. It is a straightforward assignment - synonymous to the = operator; so what you ended up with as value was the string "Legal business name.title()". You should use the Evaluate keyword like in the second call, which does python's code eval.
The other mistake was to use two different variables - you store the capitalized version in the var ${temp_answer}, but then you don't remove the whitespace from it, but from the original one - ${string_1}. So even if the capitalization worked, you still wouldn't get the desired end result in the ${answer} var.
Here's a one-liner how to achieve what you need:
${answer}= evaluate """${string_1}""".title().replace(" ","")
The 2 methods are chained - replace() works on the result of title(), and the value of string_1 is in triple quotes so python works with its sting representation.

Concatenation with empty string raises ERR:INVALID DIM

In TI-BASIC, the + operation is overloaded for string concatenation (in this, if nothing else, TI-BASIC joins the rest of the world).
However, any attempt to concatenate involving an empty string raises a Dimension Mismatch error:
"Fizz"+"Buzz"
FizzBuzz
"Fizz"+""
Error
""+"Buzz"
Error
""+""
Error
Why does this occur, and is there an elegant workaround? I've been using a starting space and truncating the string when necessary (doesn't always work well) or using a loop to add characters one at a time (slow).
The best way depends on what you are doing.
If you have a string (in this case, Str1) that you need to concatenate with another (Str2), and you don't know if it is empty, then this is a good general-case solution:
Str2
If length(Str1
Str1+Str2
If you need to loop and add a stuff to the string each time, then this is your best solution:
Before the loop:
" →Str1
In the loop:
Str1+<stuff_that_isn't_an_empty_string>→Str1
After the loop:
sub(Str1,2,length(Str1)-1→Str1
There are other situations, too, and if you have a specific situation, then you should post a simplified version of the relevant code.
Hope this helps!
It is very unfortunate that TI-Basic doesn't support empty strings. If you are starting with an empty string and adding chars, you have to do something like this:
"?
For(I,1,3
Prompt Str1
Ans+Str1
End
sub(Ans,2,length(Ans)-1
Another useful trick is that if you have a string that you are eventually going to evaluate using expr(, you can do "("+Str1+")"→Str1 and then freely do search and replace on the string. This is a necessary workaround since you can't search and replace any text involving the first or last character in a string.

string combine and split algorithm

I am using redis storing data, datas are combinated sequences of values, a sequece is combined with separator : and several string values, for example:
value1:value2:value3
the problem is those values may contain : in them, my first thought is escaping : to :: in the values, and then combine them, and I can split them by a solo :.
but this is not perfect, because {'abc', 'aaa:', 'bbb'} will be escaped to {'abc', 'aaa::', 'bbb'} and combined to abc:aaa:::bbb, it's unresolveable. this is probably a stupid question, I'm stuck, how would you solve the problem, or any better suggestion ?
I would instead suggest enclosing the values while inserting using a special identifier towards the beginning and end of string each and then combining them. e.g :
{'%abc%', '%aaa:%', '%bbb%'}
So whenever you want to split them again you can split them using your separator and then replace the prepended and appended value as per you convention to get the original string.
Hope that Helps!

What's the point of nesting brackets in Lua?

I'm currently teaching myself Lua for iOS game development, since I've heard lots of very good things about it. I'm really impressed by the level of documentation there is for the language, which makes learning it that much easier.
My problem is that I've found a Lua concept that nobody seems to have a "beginner's" explanation for: nested brackets for quotes. For example, I was taught that long strings with escaped single and double quotes like the following:
string_1 = "This is an \"escaped\" word and \"here\'s\" another."
could also be written without the overall surrounding quotes. Instead one would simply replace them with double brackets, like the following:
string_2 = [[This is an "escaped" word and "here's" another.]]
Those both make complete sense to me. But I can also write the string_2 line with "nested brackets," which include equal signs between both sets of the double brackets, as follows:
string_3 = [===[This is an "escaped" word and "here's" another.]===]
My question is simple. What is the point of the syntax used in string_3? It gives the same result as string_1 and string_2 when given as an an input for print(), so I don't understand why nested brackets even exist. Can somebody please help a noob (me) gain some perspective?
It would be used if your string contains a substring that is equal to the delimiter. For example, the following would be invalid:
string_2 = [[This is an "escaped" word, the characters ]].]]
Therefore, in order for it to work as expected, you would need to use a different string delimiter, like in the following:
string_3 = [===[This is an "escaped" word, the characters ]].]===]
I think it's safe to say that not a lot of string literals contain the substring ]], in which case there may never be a reason to use the above syntax.
It helps to, well, nest them:
print [==[malucart[[bbbb]]]bbbb]==]
Will print:
malucart[[bbbb]]]bbbb
But if that's not useful enough, you can use them to put whole programs in a string:
loadstring([===[print "o m g"]===])()
Will print:
o m g
I personally use them for my static/dynamic library implementation. In the case you don't know if the program has a closing bracket with the same amount of =s, you should determine it with something like this:
local c = 0
while contains(prog, "]" .. string.rep("=", c) .. "]") do
c = c + 1
end
-- do stuff

Select substring between two characters in Scala

I'm getting a garbled JSON string from a HTTP request, so I'm looking for a temp solution to select the JSON string only.
The request.params() returns this:
[{"insured_initials":"Tt","insured_surname":"Test"}=, _=1329793147757,
callback=jQuery1707229194729661704_1329793018352
I would like everything from the start of the '{' to the end of the '}'.
I found lots of examples of doing similar things with other languages, but the purpose of this is not to only solve the problem, but also to learn Scala. Will someone please show me how to select that {....} part?
Regexps should do the trick:
"\\{.*\\}".r.findFirstIn("your json string here")
As Jens said, a regular expression usually suffices for this. However, the syntax is a bit different:
"""\{.*\}""".r
creates an object of scala.util.matching.Regex, which provides the typical query methods you may want to do on a regular expression.
In your case, you are simply interested in the first occurrence in a sequence, which is done via findFirstIn:
scala> """\{.*\}""".r.findFirstIn("""[{"insured_initials":"Tt","insured_surname":"Test"}=, _=1329793147757,callback=jQuery1707229194729661704_1329793018352""")
res1: Option[String] = Some({"insured_initials":"Tt","insured_surname":"Test"})
Note that it returns on Option type, which you can easily use in a match to find out if the regexp was found successfully or not.
Edit: A final point to watch out for is that the regular expressions normally do not match over linebreaks, so if your JSON is not fully contained in the first line, you may want to think about eliminating the linebreaks first.

Resources