Jump to content


Photo

Replace first space with semicolon


  • Please log in to reply
12 replies to this topic

#1 XstatyK

XstatyK
  • Members
  • 15 posts

Posted 26 May 2012 - 10:07 PM

Hi, I'm new to Autohotkey and I've been looking through the forums to try to find a solution but am not having any luck.

I'm trying to replace copied text from:
SMITH JOHN L
or
SMITH JOHN
To this:
Smith;John L
or
Smith;John
(respectively)

I have the following which will change the capitalization to what I need
but am stuck on trying to remove only the first space and replace it with a semicolon
; Convert text from clipboard

^\:: ; Convert text
StringUpper Clipboard, Clipboard, T
Send %Clipboard%
RETURN


; End Convert text


#2 Maestr0

Maestr0
  • Members
  • 649 posts

Posted 26 May 2012 - 10:15 PM

Like this?
; Convert text from clipboard

^\:: ; Convert text
StringUpper string, clipboard, T
StringReplace, string, string, %A_Space%, `;
Send % string
RETURN


; End Convert text


#3 XstatyK

XstatyK
  • Members
  • 15 posts

Posted 26 May 2012 - 10:23 PM

Thanks Maestr0
Ok so that changes
SMITH JOHN L to Smith John;L

How can we change the first space to a semicolon and not the second one so it looks like:
Smith;John L

#4 Leef_me

Leef_me
  • Moderators
  • 7704 posts

Posted 26 May 2012 - 10:32 PM

Thanks Maestr0
Ok so that changes
SMITH JOHN L to Smith John;L

No, it doesn't. You are mistaken.

How can we change the first space to a semicolon and not the second one so it looks like:
Smith;John L

It already does this.

Please make sure you are testing the correct script.

I just now pasted this first line from my clipboard, and the second using the Maestr0's script.
SMITH JOHN L
Smith;John L

#5 XstatyK

XstatyK
  • Members
  • 15 posts

Posted 26 May 2012 - 10:49 PM

I was copying the text from Excel and pasting it in a new row and that was giving me: Smith John;L

When I tried it in notepad it works fine

So I'm guessing the two cells or Excel formatting is changing the spacing somehow
Sorry for not mentioning that as I didn't know it would have an effect on it.

The copied text is from an Excel sheet with the names in only two cells in one row
Cell 1 = last name Cell 2 = First name (sometimes middle initial)

When copying the text from Excel to notepad there seems to be a tab inserted in between the last name and first name, but I'm not certain it is a tab.
If it is a tab I would need to delete it first

I would need a line before the StringReplace to delete that extra space or tab

#6 Leef_me

Leef_me
  • Moderators
  • 7704 posts

Posted 26 May 2012 - 11:14 PM

As a convenience for users there are two predefined variables
A_Space and A_Tab :idea: fyi, the upper/lower case of the words doesn't matter.
see here <!-- m -->http://www.autohotke...les.htm#BuiltIn<!-- m -->

Since you now see that Excel is providing a tab instead of a space
You can change Maestr0's script simple by changing the variable in the one line.

Try it and see how it works for you.

#7 Maestr0

Maestr0
  • Members
  • 649 posts

Posted 26 May 2012 - 11:18 PM

ah, what Leef_Me is saying.
I'm guessing what you were copying from excel was something like:
COL A: Smith
COL B: John L.

Copying columns from Excel to the clipboard divides them into tabs (%A_Tab% in AHK).
My example script replaces the first found space (%A_Space%) to a ; so in the case I described above, that's between John and L.
Try Leef_Me's suggestions, you should be able to figure it out.

#8 XstatyK

XstatyK
  • Members
  • 15 posts

Posted 26 May 2012 - 11:46 PM

Thanks Leef_me and Maestr0

Changing
StringReplace, string, string, %A_Space%, `;
to
StringReplace, string, string, %A_Tab%, `;
Does work in Excel since excel is adding formatting

The one that Maestr0 originally posted works fine without extra formatting that Excel added and I didn't think to mention.

I'll need to look more into variables to be more familiar with it, Thanks for helping me figure this out :D

#9 Leef_me

Leef_me
  • Moderators
  • 7704 posts

Posted 27 May 2012 - 12:02 AM

Congrats XstatyK !

We all want the best for you; for you to succeed.
But we will also be honest to tell you are wrong, when you are wrong.

Best wishes.

#10 XstatyK

XstatyK
  • Members
  • 15 posts

Posted 27 May 2012 - 04:32 AM

Thanks ;) I have to say that is fast support from the forum and that was cool.
I'd rather be corrected and learn the right way than to go on thinking I'm doing it right and its all wrong.

So here I added a piece. I wanted to set another hotkey to paste the name as Smith, John (adding comma and space) Using the syntax from the provided code

StringReplace, string, string, %A_Tab%, `,%A_space%

Here it is in full, copy from Excel
SMITH JOHN L
and paste as either
Smith;John L
or Smith, John L

; Convert text from clipboard

^\:: ; Convert text Uses CTRL and \ as hotkey
StringUpper string, clipboard, T ; Copies text and converts Upper case to lowercase except for first letter of word
StringReplace, string, string, %A_Tab%, `; ; Replaces Tab with semicolon
Send % string 
RETURN

^]:: ; Convert text Uses CTRL and ] as hotkey
StringUpper string, clipboard, T ; Copies text and converts Upper case to lowercase except for first letter of word
StringReplace, string, string, %A_Tab%, `,%A_space% ; Replaces Tab with comma and a space
Send % string
RETURN

; End Convert text

Just wanted to share the final, let me know if my comments are wrong and I'll correct.

:wink:

#11 Maestr0

Maestr0
  • Members
  • 649 posts

Posted 27 May 2012 - 08:35 AM

Nope, that's excellent. Good job!
Thing to note though, not every character to replace (with) will need a ' in front of it (though both ; and , do need it).

Glad I could help, have fun with ahk!

#12 sinkfaze

sinkfaze
  • Moderators
  • 6089 posts

Posted 27 May 2012 - 02:31 PM

Since both of your hotkeys do the same thing except for one step, you could "stack" the hotkeys in your script and adjust the one operation based on what hotkey was pressed:

^\::	[color=#00BF00]; Convert tab to semicolon Uses CTRL and \ as hotkey[/color]
^]::	[color=#00BF00]; Convert tab to comma/space Uses CTRL and ] as hotkey[/color]
StringUpper string, clipboard, T [color=#00BF00]; Copies text and converts Upper case to lowercase except for first letter of word[/color]
[color=#FF0000]if	A_ThisLabel=^\[/color]	[color=#00BF00]; tells you which hotkey was pressed, see http://www.autohotkey.com/docs/Variables.htm#ThisLabel[/color]
	StringReplace, string, string, %A_Tab%, `; [color=#00BF00]; Replaces Tab with semicolon[/color]
[color=#FF0000]else[/color]	StringReplace, string, string, %A_Tab%, `,%A_space% [color=#00BF00]; Replaces Tab with comma and a space[/color]
Send % string
RETURN


#13 XstatyK

XstatyK
  • Members
  • 15 posts

Posted 29 May 2012 - 03:16 AM

Hey thats cool I'll have to give it a try, this can actually be fun, probably a lot more as I learn more. I will try this out