Concat string in the middle of another String, skipping the last - dart

I want to transform this String "23 março 2019" into "23 de março de 2019".
I did the way below and it works, but I have to type a lot to get rid of the 'in case first' "de".
String strDate = splitted[0];
var fullDate = strDate
.split(" ")
.map((s) => " de " + s)
.join()
.trim()
.split(' ')
.skip(1)
.join(' ');
Is there a way to simplify this expression maybe doing the equivalent to (skipLast function doesn't exits):
var fdate = strDate.split(" ").map((s) => s+" de ").skipLast().join();

Why don't you try replacing the blank spaces with your value, like this:
final data = "23 março 2019";
final result = data.replaceAll(" ", " de ");
print(result);

Related

Convert month (date) into full month (string)

I have a date field (adate10) called Period with values like 07/02/2018.
I need to convert this into a string like "July 2, 2018."
I can use the code below to produce a string, Period_String, that says "JUL 2, 2018." I can't figure out how to format the month as the full month, e.g., "July." Is the only option to create an if/then statement that says 'if 1 then "January", if 2, then "February"', etc.? Was hoping there was a built in format but can't find it.
* extract each date element, then format.
compute mo = xdate.month(Period).
compute da = xdate.mday(Period).
compute yr = xdate.year(Period).
formats mo (month) da yr (F4.0).
execute.
* concatentate date elements as strings.
string Period_String (a30).
compute Period_String = concat(ltrim(string(mo,month)), " ", ltrim(string(da,F4)), ", ", ltrim(string(yr,F4))).
execute.
see the revised month format (month9 instead of month):
compute Period_String = concat(ltrim(string(mo,month9)), " ", ltrim(string(da,F4)), ", ", ltrim(string(yr,F4))).
EDIT:
using #mirirai's one-liner suggestion + getting only first letter in capitals:
string Period_String(a20).
COMPUTE Period_String = CONCAT(
RTRIM(char.substr(STRING(period,MONTH9),1,1)),
RTRIM(lower(char.substr(STRING(period,MONTH9),2)))," ",
LTRIM(STRING(XDATE.MDAY(Period),F2)), ", ",
LTRIM(STRING(XDATE.YEAR(period),F4))
).

Checking variables exist before building an array

I am generating a string from a number of components (title, authors, journal, year, journal volume, journal pages). The idea is that the string will be a citation as so:
#citation = article_title + " " + authors + ". " + journal + " " + year + ";" + journal_volume + ":" + journal_pages
I am guessing that some components occasionally do not exist. I am getting this error:
no implicit conversion of nil into String
Is this indicating that it is trying to build the string and one of the components is nil? If so, is there a neat way to build a string from an array while checking that each element exists to circumvent this issue?
It's easier to use interpolation
#citation = "#{article_title} #{authors}. #{journal} #{year}; #{journal_volume}:#{journal_pages}"
Nils will be substituted as empty strings
array = [
article_title, authors ,journal,
year, journal_volume, journal_pages
]
#citation = "%s %s. %s %s; %s:%s" % array
Use String#% format string method.
Demo
>> "%s: %s" % [ 'fo', nil ]
=> "fo: "
Considering that you presumably are doing this for more than one article, you might consider doing it like so:
SEPARATORS = [" ", ". ", " ", ";", ":", ""]
article = ["Ruby for Fun and Profit", "Matz", "Cool Tools for Coders",
2004, 417, nil]
article.map(&:to_s).zip(SEPARATORS).map(&:join).join
# => "Ruby for Fun and Profit Matz. Cool Tools for Coders 2004;417:"

Lua gsub second instance

I'm using
local mystring = 'Thats a really nice house.'
string.gsub(mystring,"% ", "/",1)
to replace the first white space character with an slash.
But how to replace only the second occurrence of the white space?
You can use a function as replacement value in string.gsub and count the matches yourself:
local mystring = "Thats a really nice house."
local cnt = 0
print( string.gsub( mystring, " ", function( m )
cnt = cnt + 1
if cnt == 2 then
return "/"
end
end ) )
Try string.gsub(mystring,"(.- .-) ", "%1/",1).
You can replace the first instance with something else (assuming the replacement is not present in the string itself, which you can check), then replace it back:
print(mystring:gsub("% ", "\1",1):gsub("% ", "/",1):gsub("\1","% ", 1))
This prints: Thats a/really nice house.. Also, you don't need to escape spaces with %.

neo4j query extremly slow

I want to create a graph using the neo4jclient , I have a dictionary that hold 2 kinds of object ( documents ,list) ,
Dictionary<Document, List<Concept>>
as you can see ,each document hold a list of concept ,a relationship must be between them (document)<-[:IN]-(concept)
public void LoadNode(Dictionary<Document, List<Concept>> dictionary, GraphClient _client)
{
var d = dictionary.ToList();
var t = dictionary.Values;
string merge1 = string.Format
("MERGE (source:{0} {{ {1}:row.Key.Id_Doc }})", "Document", "Name");
string strForEachDoc = " FOREACH( concept in row.Value | ";
string merge2 = string.Format
("MERGE (target:{0} {{ {1} : concept.Name }})", "Concept", "Name");
string merge3 = string.Format
(" MERGE (source)-[ r:{0} ]->(target)", "Exist");
{_client.Cypher
.WithParam("coll", d)
.ForEach("(row in {coll} | " +
merge1 + " " +
strForEachDoc + " " +
merge2 + " " +
merge3 + "))")
.ExecuteWithoutResults();
}
}
it takes times and Visual studio ran into a bizarre error
"Une exception de première chance de type 'System.AggregateException'
s'est produite dans mscorlib.dll"

Parsing POST request with unexpected URL encoding

This question follows an earlier one.
Here is some code that reproduces the problem:
POST:
str = "accountRequest=<NewUser>" & vbLf & _
"Hello" & vbTab & "World" & vbLf & _
"</NewUser>"
Set objHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
objHTTP.open "POST", "service.asp", False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.send str
response.Write(objHTTP.responseText)
Set objHTTP = Nothing
service.asp:
function w (str)
response.Write(str & "<br>")
end function
str = request.Form("accountRequest")
w(str)
w("Tabs: "& InStr(str,vbTab))
w("Lines: "& InStr(str,vbLf))
output:
HelloWorld
Tabs: 0
Lines: 0
Can anyone please help?
Try:
Replace(Request.Form("accountRequest"), vbLF, vbCRLF))
Or:
Replace(Request.Form("accountRequest"), vbLF, "<br>"))|
Depending on where you're displaying it, either should work.
Or possibly this:
Function URLDecode(sConvert)
Dim aSplit
Dim sOutput
Dim I
If IsNull(sConvert) Then
URLDecode = ""
Exit Function
End If
' convert all pluses to spaces
sOutput = REPLACE(sConvert, "+", " ")
' next convert %hexdigits to the character
aSplit = Split(sOutput, "%")
If IsArray(aSplit) Then
sOutput = aSplit(0)
For I = 0 to UBound(aSplit) - 1
sOutput = sOutput & _
Chr("&H" & Left(aSplit(i + 1), 2)) &_
Right(aSplit(i + 1), Len(aSplit(i + 1)) - 2)
Next
End If
URLDecode = sOutput
End Function
From here: http://www.aspnut.com/reference/encoding.asp
If they are coming across the wire as the actual "\" and "n" characters, you can do a replace on those characters with the appropriate vbCRLF and vbTAB constants.
Finally figured out that ASP Request.Form method doesn't preserve tabs if they're in the "\t" format (as opposed to URL encoded). However, PHP's $_POST does.

Resources