Quantcast
Channel: Capitalize only first character of string and leave others alone? (Rails) - Stack Overflow
Browsing all 19 articles
Browse latest View live

Answer by justi for Capitalize only first character of string and leave...

"i'm from New York".camelize=> "I'm from New York"

View Article



Answer by Dragonn steve for Capitalize only first character of string and...

Rails starting from version 5.2.3 has upcase_first method.For example, "my Test string".upcase_first will return My Test string.

View Article

Answer by user1519240 for Capitalize only first character of string and leave...

As of Rails 5.0.0.beta4 you can use the new String#upcase_firstmethod or ActiveSupport::Inflector#upcase_first to do it. Check this blog post for more info.So"i'm from New York...".upcase_firstWill...

View Article

Answer by Emil Laine for Capitalize only first character of string and leave...

str.sub(/./, &:capitalize)

View Article

Answer by JVK for Capitalize only first character of string and leave others...

If and only if OP would want to do monkey patching on String object, then this can be usedclass String # Only capitalize first letter of a string def capitalize_first self.sub(/\S/, &:upcase)...

View Article


Answer by Pavel Pravosud for Capitalize only first character of string and...

my_string = "hello, World"my_string.sub(/\S/, &:upcase) # => "Hello, World"

View Article

Answer by Lukas Baliak for Capitalize only first character of string and...

Perhaps the easiest way.s = "test string"s[0] = s[0].upcase# => "Test string"

View Article

Answer by Lasse Bunk for Capitalize only first character of string and leave...

str = "this is a Test"str.sub(/^./, &:upcase)# => "This is a Test"

View Article


Answer by Paul.s for Capitalize only first character of string and leave...

Most of these answers edit the string in place, when you are just formatting for view output you may not want to be changing the underlying string so you can use tap after a dup to get an edited...

View Article


Answer by lmanners for Capitalize only first character of string and leave...

An object oriented solution:class String def capitalize_first_char self.sub(/^(.)/) { $1.capitalize } endendThen you can just do this:"i'm from New York".capitalize_first_char

View Article

Answer by Bartuzz for Capitalize only first character of string and leave...

You can use humanize.If you don't need underscores or other capitals in your text lines.Input:"i'm from New_York...".humanizeOutput:"I'm from new york..."

View Article

Answer by mahemoff for Capitalize only first character of string and leave...

No-one's mentioned gsub, which lets you do this concisely.string.gsub(/^([a-z])/) { $1.capitalize }Example:> 'caps lock must go'.gsub(/^(.)/) { $1.capitalize }=> "Caps lock must go"

View Article

Answer by frontendbeauty for Capitalize only first character of string and...

Note that if you need to deal with multi-byte characters, i.e. if you have to internationalize your site, the s[0] = ... solution won't be adequate. This Stack Overflow question suggests using the...

View Article


Answer by Saim for Capitalize only first character of string and leave others...

An even shorter version could be:s = "i'm from New York..."s[0] = s.capitalize[0]

View Article

Answer by Pascal Van Hecke for Capitalize only first character of string and...

This should do it:title = "test test"title[0] = title[0].capitalizeputs title # "Test test"

View Article


Answer by Taryn East for Capitalize only first character of string and leave...

Titleize will capitalise every word.This line feels hefty, but will guarantee that the only letter changed is the first one.new_string = string.slice(0,1).capitalize +...

View Article

Answer by maček for Capitalize only first character of string and leave...

Edit 2I can't seem to replicate your trouble. Go ahead and run this native Ruby script. It generates the exact output your looking for, and Rails supports all of these methods. What sort of inputs are...

View Article


Answer by Jeriko for Capitalize only first character of string and leave...

string = "i'm from New York"string.split(/\s+/).each{ |word,i| word.capitalize! unless i > 0 }.join('')# => I'm from New York

View Article

Capitalize only first character of string and leave others alone? (Rails)

I'm trying to get Rails to capitalize the first character of a string, and leave all the others the way they are. I'm running into a problem where "i'm from New York" gets turned into "I'm from new...

View Article
Browsing all 19 articles
Browse latest View live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>