← Back to Ruby Course | Chapter 3: Strings | Lesson 1 of 7

String Methods का परिचय

String methods text के लिए built-in tools हैं जो case बदल सकते हैं, length measure कर सकते हैं, या spaces trim कर सकते हैं। यह हर word के लिए एक toolbox ready रखने जैसा है।

In this page:

  1. String Methods
Syntax
ruby
string.length
string.upcase
string.downcase
string.include?("text")
string.reverse

String Methods

Ruby strings transformation और inspection के लिए built-in methods का एक बड़ा set के साथ आती हैं, जैसे .upcase, .downcase, .reverse, .length, और .strip।

ज़्यादातर string methods original modify करने की बजाय एक नई string return करते हैं, non-mutating operations के लिए Ruby की general preference follow करते हुए। ! से खत्म होने वाले methods (जैसे .upcase!) इसकी बजाय string को in place mutate करते हैं।

Note: ! से खत्म होने वाला एक method (जैसे upcase!) आमतौर पर receiver को in place mutate करता है -- non-! version इसकी बजाय एक नया object return करता है।

उदाहरण: String Methods

markup
text = "  Hello, Ruby!  "

puts text.strip
puts text.strip.upcase
puts text.strip.length
puts text.strip.reverse
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि text.strip text बदल देगा, जब यह एक नई string return करता है जब तक आप strip! इस्तेमाल न करें।
  2. upcase! जैसा एक bang method call करना और result इस्तेमाल करना, जब कुछ न बदलने पर यह nil return करता है।
  3. length को bytes की संख्या से confuse करना, जब Ruby characters count करता है (bytes के लिए bytesize इस्तेमाल करें)।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.