日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

substr summary

發布時間:2024/1/18 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 substr summary 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
substr總結: substr EXPR,OFFSET,LENGTH,REPLACEMENT substr EXPR,OFFSET,LENGTH substr EXPR,OFFSET 1、從一個字符串中提取一個子字符串并返回它。 OFFSET是起始位置,為正時從前面開始數,為負時從后面開始數,數的方向都是從起始位置向后面數。省略時默認值為0。 LENGTH 是向后數的長度,直接向后數length個字符,超出字符串長度時直接數到字符串結尾。為負時,從offset數到從字符串結尾向前數的length后結束。省略時默認為數到結尾。 my $s = "The black cat climbed the green tree"; my $color = substr $s, 4, 5;????? # black? 從第4個字符開始向后數5個。 my $middle = substr $s, 4, -11; # black cat climbed the從第4個字符開始數到倒數第11個字符結束。不包含倒數第11個字符。 my $end = substr $s, 14;??????? # climbed the green tree 從第14個字符數到結束。 my $tail = substr $s, -4;??????? # tree 從倒數第4個字符數到結束。 my $z = substr $s, -4, 2;???? # tr 從倒數第4個字符向后數2個字符。 2、substr()函數本身可以作為左值。如果指定的長度比length短,string將會變短,反之變長,這里相當于替換,用右值替換substr指定的字符串。為了保證相同長度,你可以用sprintf來賦值。 用offset和length指定的字串部分超出母串,只返回為超出母串的部分,不會出現警告。如果初始位置超出母串,那么會出現一個警告,返回未定義的值,同時會出現警告,這是作為左值時,會出現異常。下面是這幾種情況的例子: my $name = 'fred'; substr($name, 4) = 'dy';???????? # $name is now 'freddy' my $null = substr $name, 6, 2;?? # returns "" (no warning) my $oops = substr $name, 7;????? # returns undef, with warning substr($name, 7) = 'gap';??????? # raises an exception 3、用substr的第4個參數可以用作替換操作,替換母串中部分字符然后返回替換后的字串,功能和splice()類似。 my $s = "The black cat climbed the green tree"; my $z = substr $s, 14, 7, "jumped from";??? # climbed # $s is now "The black cat jumped from the green tree" 4、由前三個參數組成的substr()函數作為左值時,可以用作“magic bullet”,每次被執行,它都會使用最新的字符串,例如: $x = '1234'; for (substr($x,1,2)) { $_ = 'a';?? print $x,"\n";??? # prints 1a4 $_ = 'xyz'; print $x,"\n";??? # prints 1xyz4 $x = '56789'; $_ = 'pq';? print $x,"\n";??? # prints 5pq9 } 此時相當于: my $x = '1234'; { substr($x,1,2) = 'a'; print $x,"\n"; substr($x,1,2) = 'xyz'; print $x,"\n"; $x = '56789'; substr($x,1,2) = 'pq';? print $x,"\n"; } ? Extracts a substring out of EXPR and returns it. First character is at offset 0 (or whatever you've set $[ to (but <don't do that)). If OFFSET is negative (or more precisely, less than $[ ), starts that far back from the end of the string. If LENGTH is omitted, returns everything through the end of the string. If LENGTH is negative, leaves that many characters off the end of the string. You can use the substr() function as an lvalue, in which case EXPR must itself be an lvalue. If you assign something shorter than LENGTH, the string will shrink, and if you assign something longer than LENGTH, the string will grow to accommodate it. To keep the string the same length, you may need to pad or chop your value using sprintf. If OFFSET and LENGTH specify a substring that is partly outside the string, only the part within the string is returned. If the substring is beyond either end of the string, substr() returns the undefined value and produces a warning. When used as an lvalue, specifying a substring that is entirely outside the string raises an exception. Here's an example showing the behavior for boundary cases: An alternative to using substr() as an lvalue is to specify the replacement string as the 4th argument. This allows you to replace parts of the EXPR and return what was there before in one operation, just as you can with splice(). Note that the lvalue returned by the three-argument version of substr() acts as a 'magic bullet'; each time it is assigned to, it remembers which part of the original string is being modified; for example:

總結

以上是生活随笔為你收集整理的substr summary的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。