Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 345 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ruby: How to get the first character of a string

#1
How can I get the first character in a string using Ruby?

Ultimately what I'm doing is taking someone's last name and just creating an initial out of it.

So if the string was "Smith" I just want "S".
Reply

#2
Other way around would be using the [chars][1] for a string:

```ruby
def abbrev_name
first_name.chars.first.capitalize + '.' + ' ' + last_name
end
```


[1]:

[To see links please register here]

Reply

#3
If you're using `Rails` You can also use [`truncate`](

[To see links please register here]

)

> 'Smith'.truncate(1, omission: '')
#=> "S"

or for additional formatting:

> 'Smith'.truncate(4)
#=> "S..."

> 'Smith'.truncate(2, omission: '.')
#=> "S."


While this is definitely overkill for the original question, for a pure `ruby` solution, here is how `truncate` is implemented in `rails`

# File activesupport/lib/active_support/core_ext/string/filters.rb, line 66
def truncate(truncate_at, options = {})
return dup unless length > truncate_at

omission = options[:omission] || "..."
length_with_room_for_omission = truncate_at - omission.length
stop = if options[:separator]
rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
else
length_with_room_for_omission
end

"#{self[0, stop]}#{omission}"
end
Reply

#4
Try this:

```
def word(string, num)
string = 'Smith'
string[0..(num-1)]
end
```
Reply

#5
Another option that hasn't been mentioned yet:

> "Smith".slice(0)
#=> "S"
Reply

#6
Try this:

>> a = "Smith"
>> a[0]
=> "S"

**OR**

>> "Smith".chr
#=> "S"
Reply

#7
Any of these methods will work:

name = 'Smith'
puts name.[0..0] # => S
puts name.[0] # => S
puts name.[0,1] # => S
puts name.[0].chr # => S
Reply

#8
In Rails

name = 'Smith'
name.first
Reply

#9
You can use Ruby's open classes to make your code much more readable. For instance, this:

class String
def initial
self[0,1]
end
end

will allow you to use the `initial` method on any string. So if you have the following variables:

last_name = "Smith"
first_name = "John"


Then you can get the initials very cleanly and readably:

puts first_name.initial # prints J
puts last_name.initial # prints S

The other method mentioned here doesn't work on Ruby 1.8 (not that you should be using 1.8 anymore anyway!--but when this answer was posted it was still quite common):

puts 'Smith'[0] # prints 83

Of course, if you're not doing it on a regular basis, then defining the method might be overkill, and you could just do it directly:

puts last_name[0,1]
Reply

#10
For completeness sake, since Ruby 1.9 String#chr returns the first character of a string. Its still available in 2.0 and 2.1.

"Smith".chr #=> "S"

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through