Add underscore between each character of a string - string

I am currently looking for a way in which I can add a underscore
in a string generated as such:
>>> bin(1)[2:].zfill(8)
'00000001'
I want the string to be '0_0_0_0_0_0_0_1'
How do i add the underscore between each character?

This can be achieved in multiple ways and has already been answered a lot of times here. Usually people seem to do to some kind of replace operation but there are other ways. Here's how to use the String join() method.
s = "00000001"
"_".join(s)

Related

How to use the split() method with some condition?

There is one condition where I have to split my string in the manner that all the alphabetic characters should stay as one unit and everything else should be separated like the example shown below.
Example:
Some_var='12/1/20 Balance Brought Forward 150,585.80'
output_var=['12/1/20','Balance Brought Forward','150,585.80']
Yes, you could use some regex to get over this.
Some_var = '12/1/20 Balance Brought Forward 150,585.80'
match = re.split(r"([0-9\s\\\/\.,-]+|[a-zA-Z\s\\\/\.,-]+)", Some_var)
print(match)
You will get some extra spaces but you can trim that and you are good to go.
split isn't gonna cut it. You might wanna look into Regular Expressions (abbreviated regex) to accomplish this.
Here's a link to the Python docs: re module
As for a pattern, you could try using something like this:
([0-9\s\\\/\.,-]+|[a-zA-Z\s\\\/\.,-]+)
then trim each part of the output.

Divide the string with a dot in Groovy

How can I divide a string with dots as delimiters in Groovy?
If I have a string like "22112018", how do I convert it to "22.11.2018"?
EDIT:
I wasn't really sure how to formulate the question. I wanted to 'split' the string but split() method doesn't do what I need (doesn't mean the same).
This answer in comments (by #ernest_k) was good enough for what I needed:
text = "22112018"
"${text[0..1]}.${text[2..3]}.${text[4..7]}"
However, it was not an "answer" in the SO way, so I'm accepting the answer by #tim_yates (also works and is probably a more precise and robust solution).
I assume this is a date...
You could do:
Date.parse('ddMMyyyy', '22112018').format('dd.MM.yyyy')
instead of just grabbing characters

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!

MATLAB line continuation within 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.

Resources