Strings की तुलना
Strings math symbols की बजाय eq और lt जैसे word-like operators इस्तेमाल करती हैं।
In this page:
Strings Compare करना
String comparison operators हैं eq, ne, lt, gt, le, और ge, और cmp -1, 0, या 1 return करता है। ये character codes इस्तेमाल करके character by character compare करते हैं, इसलिए uppercase letters lowercase वालों से पहले sort होते हैं। non-numeric strings पर == जैसे numeric operators इस्तेमाल करना warnings produce करता है और इनकी numeric values compare करता है।
Note:
दोनों sides को lc से lowercase करके case-insensitively compare करें।
उदाहरण: Comparing strings
use strict;
use warnings;
print "eq: ", ("perl" eq "perl" ? "same" : "different"), "\n";
print "ne: ", ("perl" ne "Perl" ? "different" : "same"), "\n";
print "lt: ", ("apple" lt "banana" ? "yes" : "no"), "\n";
print "Zebra lt apple: ", ("Zebra" lt "apple" ? "yes" : "no"), "\n";
print "cmp: ", join(",", "a" cmp "b", "b" cmp "b", "c" cmp "b"), "\n";
print "case-insensitive: ", (lc("PERL") eq lc("perl") ? "equal" : "not equal"), "\n";
print "sorted: ", join(" ", sort qw(banana Apple cherry apple)), "\n";
print "sorted ignoring case: ", join(" ", sort { lc($a) cmp lc($b) or $a cmp $b } qw(banana Apple cherry apple)), "\n";
# Output:
# eq: same
# ne: different
# lt: yes
# Zebra lt apple: yes
# cmp: -1,0,1
# case-insensitive: equal
# sorted: Apple apple banana cherry
# sorted ignoring case: Apple apple banana cherry
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- strings compare करने के लिए == इस्तेमाल करना
- यह भूल जाना कि "Zebra" "apple" से पहले sort होता है
- lt की बजाय < से text compare करना
चैप्टर सारांश
- eq और ne strings की equality test करते हैं
- lt gt le ge strings order करते हैं
- cmp -1, 0, या 1 return करता है
- Uppercase lowercase से पहले sort होता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: