Skip to content

Instantly share code, notes, and snippets.

@radixdev
Created June 2, 2017 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radixdev/1879ceac52f6cf2eac6acaa59002646d to your computer and use it in GitHub Desktop.
Save radixdev/1879ceac52f6cf2eac6acaa59002646d to your computer and use it in GitHub Desktop.
Converts string format to string concat
#!/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)
@radixdev
Copy link
Author

radixdev commented Jun 2, 2017

String Format To Concatenation Converter

Usage:

  1. Copy the entire String.format() and it's contents to your clipboard.
  2. Run ruby string_format_to_concat.rb.
  3. The output is pasted to your clipboard.

Description: Android Studio doesn't have a refactor tool to convert String.format() calls to their concatenated form.
E.g.

String.format("The manifest does not define the %s permission.", applicationMessagePermission)
=>
"The manifest does not define the " + applicationMessagePermission + " permission."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment