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

आम String Functions

string library text slice, case change, repeat, और reverse कर सकती है, और methods colon syntax से काम करते हैं।

In this page:

  1. Common String Functions

Common String Functions

string.sub(s, i, j) एक slice return करता है, और negative indexes अंत से count करते हैं। string.upper, lower, rep, और reverse वही करते हैं जो इनके नाम कहते हैं, और string.len length देता है। हर string के पास इसके metatable की तरह string library है, इसलिए s:upper() string.upper(s) जैसा ही है।

Note: Lua strings में indexes 1 से शुरू होते हैं, और sub के दोनों ends inclusive हैं।

उदाहरण: Common string functions

lua
local s = "Hello, Lua"
print(s:sub(1, 5), s:sub(-3), s:sub(8))
print(s:upper(), s:lower())
print(("ab"):rep(3, "-"), s:reverse())
print(s:byte(1), string.char(72, 105))
print(s:len(), s)

-- Output:
-- Hello	Lua	Lua
-- HELLO, LUA	hello, lua
-- ab-ab-ab	auL ,olleH
-- 72	Hi
-- 10	Hello, Lua
{# 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. indexes को 0 से शुरू करना
  2. यह भूल जाना कि sub end position include करता है
  3. यह उम्मीद करना कि upper original string modify करेगा
चैप्टर सारांश
  • string.sub 1-based inclusive indexes से slice करता है
  • Negative indexes अंत से count करते हैं
  • s:upper() method form है
  • Functions नई strings return करते हैं

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.