Chapter 4.2.5 Back to the title helper. Why do we need to use string concatenation instead of string interpolation? - railstutorial.org

String Concatenation Instead of String Interpolation.
Why do we have to use string concatenation instead of string interpolation? I think in my own honest opinion. String interpolation is one of the magic of ruby on rails while string concatenation is just an ordinary one.
kindly please explain why is it use in this chapter and what is the advantage of over the other and vice versa.
Thanks
-Eric John Iglesia

String concatenation is faster (needs less processing power) and safer because you will not need to match parameters with format string and so. With interpolation, you may later remove a parameter (variable) without its matching placeholder in the format string. Concatenation accepts null values as well, this is not possible in a format string.

Related

Convert comma separated non json string to json

Below is the value of a string in a text column.
select col1 from tt_d_tab;
'A:10000000,B:50000000,C:1000000,D:10000000,E:10000000'
I'm trying to convert it into json of below format.
'{"A": 10000000,"B": 50000000,"C": 1000000,"D": 10000000,"E": 10000000}'
Can someone help on this?
If you know that neither the keys nor values will have : or , characters in them, you can write
select json_object(regexp_split_to_array(col1,'[:,]')) from tt_d_tab;
This splits the string on every colon and comma, then interprets the result as key/value pairs.
If the string manipulation gets any more complicated, SQL may not be the ideal tool for the job, but it's still doable, either by this method or by converting the string into the form you need directly and then casting it to json with ::json.
If your key is a single capital letter as in your example
select concat('{',regexp_replace('A:10000000,B:50000000,C:1000000,D:10000000,E:10000000','([A-Z])','"\1"','g'),'}')::json json_field;
A more general case with any number of letters caps or not
select concat('{',regexp_replace('Ac:10000000,BT:50000000,Cs:1000000,D:10000000,E:10000000','([a-zA-Z]+)','"\1"','g'),'}')::json json_field;

Talend: Converting a string to BigDecimal

I am trying to convert a string number from one MySQL table to another.
I have used the following on many occasions, however it does not seem to be working in this instance and I am unsure as to why. The string it is converting is 50,000.00.
With that formula in tMap it produces the following error:
When I look at the code on 3031:
So something is just not functioning as I expect. Any help would be great.
In regex syntax, "$" indicates the end of the string. In your regex, you are trying to remove any character after the end of the string which is not a number or a dot, which won't work, so the "," is never removed from your string, resulting in a conversion error.
You can do this:
new BigDecimal(row1.Trade_Amount.replaceAll("[^\\d.]", ""))

How can I put single quotes in a string?

I'm facing a problem with string in MATLAB the default string is C:\Users\Root\Downloads\Path. I want to make this string with single quotes inside it like this 'C:\Users\Root\Downloads\Path\'. I try many times to escape the string with backslash like other programming languages but MATLAB didn't doing this i don't know how to fix this problem.
Code:
clear all
clc
s='C:\Users\Root\Downloads\Path';
str=fprintf('%s',s);
The trick is to use two quotes instead of one:
s='''C:\Users\Root\Downloads\Path''';
str=fprintf('%s',s)
'C:\Users\Root\Downloads\Path'
str =
30
Note that str will be the number 30, since fprintf returns the number of characters it prints, not the string itself! If you just want the string, then the first line is enough.
disp(s)
'C:\Users\Root\Downloads\Path'
Note that there is no data type "String" in MATLAB. You have an array of characters.

How to use numbers as delimiters in MATLAB strsplit function

As the title suggests I'm looking to detect where the numbers are in a string and then to just take the substring from the larger string. EG
If I have say zero89 or eight78, I would just like zero or nine returned. When using the strsplit function I have:
strsplit('zero89', but what do I put here?)
Interested in regexp that will provide you more options to explore with?
Extract numeric digits -
regexp('zero89','\d','match')
Extract anything other than digits -
regexp('zero89','\d+','Split')
strsplit('zero89', '\d', 'DelimiterType', 'RegularExpression')
Or just using regexp:
regexp('zero89','\D+','match')
I got the \D+ from here
Assuming you mean this strsplit?
strsplit('zero89', '8')

hstore value with single quote

I asked similar question here for: hstore value with space. And get solved by user: Clodoaldo Neto. Now I have come across next case with string containing single quote.
SELECT 'k=>"name", v=>"St. Xavier's Academy"'::hstore;
I tried it by using dollar-quoted string constant by reading http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
SELECT 'k=>"name", v=>$$St. Xavier's Academy$$'::hstore;
But I couldn't get it right.
How to make postgresql hstore using strings containing single quote?
It seems like there are more such exceptions possible for this query. How to address them all at once?
You can escape the embedded single quote that same way you'd escape any other single quote inside a string literal: double it.
SELECT 'k=>"name", v=>"St. Xavier''s Academy"'::hstore;
-- ------------------------------^^
Alternatively, you could dollar quote the whole string:
SELECT $$k=>"name", v=>"St. Xavier's Academy"$$::hstore;
Whatever interface you're using to talk to PostgreSQL should be taking care of these quoting and escaping issues. If you're using manual string wrangling to build your SQL then you should be using your driver's quoting and placeholder methods.
hstore's internal parsing understands double quotes around keys:
Double-quote keys and values that include whitespace, commas, =s or >s.
Dollar quoting is, as you noted, for SQL string literals, hstore's parser doesn't know what they mean.