JavaScript String substr()
ဥပမာများ
စာသားမှ စာကြောင်းခွဲတစ်ခုကို ထုတ်ယူပါ။
let text = "Hello world!";
let result = text.substr(1, 4);"
ရာထူး 2 တွင် စတင်ပါ။
let result = text.substr(2);
အောက်တွင် နောက်ထပ် ဥပမာများ။
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
နည်းလမ်း သည် substr()
string တစ်ခု၏ အစိတ်အပိုင်းတစ်ခုကို ထုတ်ယူသည်။
နည်းလမ်း သည် substr()
သတ်မှတ်ထားသော အနေအထားတွင် စတင်ပြီး သတ်မှတ်ထားသော စာလုံးအရေအတွက်ကို ပြန်ပေးသည်။
နည်းလမ်းသည် မူရင်း substr()
စာကြောင်းကို မပြောင်းလဲပါ။
စာကြောင်းအဆုံးမှ ဇာတ်ကောင်များကို ထုတ်ယူရန်၊ အနုတ်လက္ခဏာ စတင်သည့် အနေအထားကို အသုံးပြုပါ။
အထားအသို
string.substr(start, length)
ကန့်သတ်ချက်များ
Parameter | Description |
start | Required. The start position. First character is at index 0. If start is greater than the length, substr() returns "". If start is negative, substr() counts from the end of the string. |
length | Optional. The number of characters to extract. If omitted, it extracts the rest of the string |
တန်ဖိုးကို ပြန်ပေးသည်။
ရိုက်ပါ။ | ဖော်ပြချက် |
ကြိုးတစ်ချောင်း | ထုတ်ယူထားသောအပိုင်းပါရှိသော စာကြောင်းတစ်ခု။ အရှည် သည် 0 သို့မဟုတ် အနှုတ် ဖြစ်ပါက ၊ စာလုံးအလွတ်တစ်ခုကို ပြန်ပေးသည်။ |
နောက်ထပ် ဥပမာများ
ပထမဆုံးသာ
let result = text.substr(0, 1);
နောက်ဆုံးတစ်ခုသာ
let result = text.substr(text.length-1, 1);
နောက်ဆုံး 6-
let result = text.substr(-5, 5);
Browser ပံ့ပိုးမှု
substr()
ECMAScript1 (ES1) အင်္ဂါရပ်တစ်ခုဖြစ်သည်။
ES1 (JavaScript 1997) ကို ဘရောက်ဆာအားလုံးတွင် အပြည့်အဝ ပံ့ပိုးထားသည်။
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |