Excel function to get chars between hyphens - excel

I have a string like:
AB-CD-EF-GH-IK
I wanna get EF between second and third hyphen.
Please help me to figure it out, Thanks

Even shorter is:
=TRIM(MID(SUBSTITUTE(A1,"-",REPT(" ",LEN(A1))),2*LEN(A1),LEN(A1)))
Regards

This will work with varying lengths of strings between the dashes. Doesn't look pretty but works.
=LEFT(REPLACE(REPLACE(A1,1,FIND("-",A1),""),1,FIND("-",REPLACE(A1,1,FIND("-",A1),"")),""),FIND("-",REPLACE(REPLACE(A1,1,FIND("-",A1),""),1,FIND("-",REPLACE(A1,1,FIND("-",A1),"")),""))-1)

Not because this is the right approach, but because shorter than (what was at the time!) the accepted Answer:
=MID(A6,FIND("-",A6,FIND("-",A6)+1)+1,FIND("-",A6,FIND("-",A6,FIND("-",A6)+1)+1)-FIND("-",A6,FIND("-",A6)+1)-1)
A small point in its favour may be that it uses only two common-or-garden functions:
MID to extract the string
FIND to find the index numbers of the relevant characters.

Related

Doing two string manipulations in puppet

I am running puppet 3.8.6
In a template, I need to truncate the last four characters then remove hyphens from a string parameter. For example "foo-bar.txt" should become "foobar".
val[0..-5] works for truncating the last four characters.
val.gsub('-','') works for removing the hyphen.
But this is a syntax error.
val[0..-5].gsub('-','')
How can I do both?
I agree with the comment on your post... I don't think your example would produce a syntax error. However, though this is a little more verbose, I find splitting to be easier to reason about than removing slices of the string. This ought to work, too:
val.split('.')[0].gsub('-','')
Edit: I somehow missed that this was inside a template. Oops! I've updated as Alex Harvey suggested in the comments.

Truncate last n characters in a string using .format()

I'm trying to truncate last 100 chars of a string using .format() and add another 100 chars long string. I thought this should be the most obvious and pythonic way:
print('{:.-100}{:100}'.format(str1, str2))
But it doesn't work.
I can achieve it this way:
print('{}{:100}'.format(str1[:-100], str2))
But I don't think it's beautiful or readable code. Of course, I can use .replace() and .strip() and .ljust() but...
So am I missing something in my original broken code? Or shall I stay with the second one? Or will it be the most pythonic way to use .replace() etc?
You can do:
print('{}{}'.format(str1[:-100], str2[:100]))
At least that way, you will be consistent.
If you want other ways to do it, this question might help: Left truncate using python 3.5 str.format? .
If I was at your place, I would accept the second one. It looks readable to me. I always truncate strings like how you did.

Randomize characters in string?

unfortunately I am struggling a bit with what I thought would be an easy task.
I am writing an autocorrect in AHK for words I frequently mistype, and instead of writing every possible way to write every word incorrectly I have written a list with words I often mistype in their correct form. I now want to take each item from that list, leave the first character as is and randomize two consectutive letters in every possible way with the exclusion "is already in list".
So in pseudo code it would be:
For each word in correctWords
{
FirstLetter = split to chararray(0)
newWord = split to chararray(>0)
randomized = Firstletter + newWord.randomizeTwoLetters
if(!correctWords.Contains(randomized); correctWords.AddToList(randomized))
}
The part I struggle with is obviously the randomizeTwoLetters(), how would you go about that?
I hope you can help me, thank you!
Pulled from the AutoHotkey Help File:
AutoCorrect 4700 Common Words
If you still want to do this yourself (in C#?) look into various algorithms like: Generating a random sequence with no repeats for starters.

String compare : Comparing 'zürich' and 'zurich' results in -1

I'm trying to do a string compare for 'zürich' and 'zurich'
Something like this:
int compareResult = String.Compare(zürich, zurich);
So what happens is that it returns -1, which causes a problem as I'm using compareResult for an if-else later.
Can someone point me to the right direction on why does this happen. Do I need to clean this first before comparing "zürich" or is it something else?
you use the method just fine, but the strings are actually different.
so, in order to make this comparison in your way, you need:
decide if this you want every comparison that uses ü and other "special" latin characters to look at them as they were the simple characters.
i.e. in every time you see ü, it will treat it as a "u"
if so, you need to do pre-processing of both the strings, and replace all special chars with regular ones.
there is another thread about it here:
How can I remove accents on a string?
hope it helped.

Complex replacement in gVim

I have been a terrible person as of late when it comes to Minecraft. I have over-modded it to the point that I need to completely re-write the IDs of them all.
The only problem is that... It'll take about a couple of hours jut to re-write them ONCE, not to mention if any of them collide with the original game. So, in order to save time, I figured I'd use Vim, but after reading through several of the helpful posts on here, I still only know a minimal amount about the replacement feature/command. Here's what I'm trying to do:
Replace this
I:exampleModnamePath.id=16389
I:exampleModnamePat2.id=19657
Etc.
With this
I:exampleModnamePath.id=20000
I:exampleModnamePath.id=20001
Etc.
This continues for a while, and to those who answer, could you please inform me of how it works, so I don't have to ask these questions all the time?
For your perusal:
:let g:num = 1
:g/\.id=\d\+$/exec 's!\.id=\d\+$!.id='.g:num.'! | let g:num=g:num+1'
This is slightly simplified version of my code for (re)numbering chapters in the ebooks.
Idea in a nutshell: use :g to run something over affected lines; use :exec to generate/run new substitution command AND increment the counter. Tried it once and was surprised to find that the trick worked. Was inspired by my previous toying with :g//s/// combo.
I'm not sure what is the rule you are using to choose which number to use for replacement but if all you need
is just a new number that doesn't collide with previous ones you could try just replacing the first digit
with something in a range not used. Something like replacing 16389 with 76389
To do that you could use this :s/Path.id=.\(.*\)/Path.id=7\1
That would search for the string Path.id= followed by a single character and then a group of more characters.
I will replace it with the string Path.id=7 and the group previously selected.
You could make it more selectiv adding letters before Path.id to match only certain types of paths.

Resources