How to insert delimiter in a string with kotlin - kotlin

I have a mac string:
mac=7A2918D5434F
And I need to convert to this:
mac=7A:29:18:D5:43:4F
How can I do that in kotlin?

If you are sure that your initial string is correct you can do something like:
"7A2918D5434F".chunked(2).joinToString(":")
chunked(2) splits the string in chunks of size 2 (can be used for any Iterable).
jointToString(":") takes a list, joins the elements to string using : as delimiter

Related

What's the best way to 'normalize' a string in Redshift?

Since my texts are in Portuguese, there are many words with accent and other special characters, like: "coração", "hambúrguer", "São Paulo".
Normally, I treat these names in Python with the following function:
from unicodedata import normalize
def string_normalizer(text):
result = normalize("NFKD", text.lower()).encode("ASCII", "ignore").decode("ASCII")
return result.replace(" ", "-")
This would replace the blank spaces with '-', replace special characters and apply a lowercase convertion. The word "coração" would become "coracao", "São Paulo" would become "Sao Paulo" and so on. Now, I'm not sure what's the best way to do this in Redshift. My solution would be to apply multiple replaces, something like this:
replace(replace(replace(lower(column), 'á', 'a'), 'ç', 'c')...
Even though this works, it doesn't look like the best solution. Is there an easy way to normalize my string?
In Redshift, you can use the translate function to normalize a string. The translate function takes three arguments: the source string, the characters to replace, and the replacement characters. You can use this function to replace all the special characters in your string with their ASCII equivalent.
For example, the following query uses the translate function to replace all the special characters in a string with their ASCII equivalent. Additionally, spaces are replaced with "-" characters.
SELECT translate('São Paulo', ' áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ', '-aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC')
This query would return the string "Sao Paulo". You can use the lower function to convert the string to lowercase.
Here's an example of how you could use these functions together to normalize a string:
SELECT lower(translate('São Paulo', ' áàãâäéèêëíìîïóòõôöúùûüçÁÀà ÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ', '-aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC'))
This query would return the string "sao-paulo".

Replace all characters to ASCII in PostgreSQL

I have an input string as follows: "ABC1134M001020000089".
I want to convert the string to integer format. One of the methods I thought of was by replacing A,B,C,M to its respective ASCII Values.
How can I achieve this in PostgreSQL?

Convert 4 Byte String Array of Hex to Decimal

I am trying to convert a string of 4 bytes in a string to an integer number in LabView, currently I am using the string subset to break apart my string and then storing it into a String Indicator on the front panel, however, I need to convert that string to an int so I can show the decimal value as well as convert it from inches to mm. Here is what I'm doing now:
I've tried converting to double using the convert string to double functions as well as I've tried splitting it up using the index array...can't quite seem to get an int out ever. Thanks!
Do as Ton Plomp said and use the Scan From String block. However, in yours, you don't want to separate with commas and with spaces. Just do:
%x%x%x%x
And that should do it for ya. Don't forget to pull the scan down so you have 4 outputs.
You can use Scan from string with the following format string:
%x
You can expand the scan from string to scan multiple items at once.

Netezza SQL - How to replace string between two Comma's, with a condition

I have a string where I want to find :N between two commas, and replace the data data between two commas with blank. Example shown below.
Currently its being done manually with exporting it into excel, making changes and importing it back into database. I'm sure there would be a better way to have this done in coding without the need of export/import.
Example:
"This is sting one:Y,this is string Two:X,This is string Three:N,This is string four:N,This is string five:X,"
Desired outcome:
"This is sting one:Y,this is string Two:X,This is string five:X,"
Would really appreciate your help.
A
I would use the sql extensions toolkit and regexp_replace.
I've found that the nz implementation of regex seems to have some issues with the ? non-greedy modifier on my version. The expression that worked here was ,[^(:N)]+:N.
FORTUNE_DB(ADMIN)=> select * from so;
COL1
--------------------------------------------------------------------------------------------------------------
This is sting one:Y,this is string Two:X,This is string Three:N,This is string four:N,This is string five:X,
FORTUNE_DB(ADMIN)=> select regexp_replace(col1,',[^(:N)]+:N','') from so;
REGEXP_REPLACE
-----------------------------------------------------------------
This is sting one:Y,this is string Two:X,This is string five:X,

How to Split String into Array for multiple characters

Here is my old script, in vb
Dim strArray As String() = str.Split(New Char() {":"C})
This works fine if the char is only :
But now I want to split for this,
:.++.:
As my str is complicated is there a way to split it ?
Edit :
My
str = hello:.++.:sAwesome Right ? yeah://.,]['; :.++.:
nvijuds789g34huve02qjgv0b0whgvn0iegvb0wvi0hn
so after split
strArray(0) = hello
strArray(1)= sAwesome Right ? yeah://.,][';
strArray(2) = nvijuds789g34huve02qjgv0b0whgvn0iegvb0wvi0hn
I think you see what I need. I am the one adding :.++.: before each part in previous functions. It is because the text almost contains every single character and I can't control it :/
thats why i used some complicated combination of characters to make it impossible for the file to contain it
You can split a string by another string using the String.Split overload (String(), StringSplitOptions).
Example:
Debug.WriteLine(String.Join("|", "A:.++.:B:.++.:C:.++.:D".Split({":.++.:"}, StringSplitOptions.RemoveEmptyEntries)))
Outputs:
A|B|C|D