Created
June 2, 2017 16:53
-
-
Save radixdev/1879ceac52f6cf2eac6acaa59002646d to your computer and use it in GitHub Desktop.
Converts string format to string concat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
def show_user_prompt(msg) | |
puts msg | |
# flush our message to display | |
STDOUT.flush | |
return STDIN.gets.chomp | |
end | |
def get_user_input | |
return show_user_prompt("Please enter the string format below") | |
return a | |
end | |
def pbcopy(input) | |
str = input.to_s | |
IO.popen('pbcopy', 'w') { |f| f << str } | |
puts "copied to clipboard" | |
str | |
end | |
def pbpaste | |
`pbpaste` | |
end | |
user_input = pbpaste() | |
if !user_input.include? "String.format" | |
puts "No 'String.format' call in string. Exiting." | |
exit() | |
end | |
puts "user input:\n" + user_input | |
last_quote = user_input.rindex("\"") | |
# puts last_quote | |
string_args = user_input[last_quote+2..-2] | |
puts "string args:\n" + string_args | |
# puts string_args.split(",") | |
arg_list = string_args.split(",").map!{|arg| arg.strip()} | |
# puts arg_list.length | |
# replace the original string with the args | |
original_string = user_input[user_input.index("\"")+1..last_quote-1] | |
# puts original_string | |
# iterate over the original and replace the | |
c = original_string | |
arg_list.each { |arg| | |
# puts arg | |
# get the first param | |
quoted_string = "\" + " + arg + " + \"" | |
# c = c.sub(/%[A-Za-z]/, quoted_string) | |
c = c.sub(/%(\.[0-9])?[A-Za-z]/, quoted_string) | |
} | |
final_output = "\"" + c + "\"" | |
# puts final_output | |
# remove any empty strings from the output | |
empty_dub_quotes = "\"\"" | |
final_output = final_output.sub("#{empty_dub_quotes} + ", "") | |
final_output = final_output.sub("+ #{empty_dub_quotes}", "") | |
final_output = final_output.strip() | |
puts "final output:\n " + final_output | |
pbcopy(final_output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
String Format To Concatenation Converter
Usage:
String.format()
and it's contents to your clipboard.ruby string_format_to_concat.rb
.Description: Android Studio doesn't have a refactor tool to convert
String.format()
calls to their concatenated form.E.g.