亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 7253 | 回復(fù): 7
打印 上一主題 下一主題

如何把一個字符串分解成單個字符 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-08-27 16:11 |只看該作者 |倒序?yàn)g覽
如題

論壇徽章:
23
15-16賽季CBA聯(lián)賽之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午馬
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16賽季CBA聯(lián)賽之山東
日期:2017-12-21 16:39:1915-16賽季CBA聯(lián)賽之廣東
日期:2016-01-19 13:33:372015亞冠之山東魯能
日期:2015-10-13 09:39:062015亞冠之西悉尼流浪者
日期:2015-09-21 08:27:57
2 [報告]
發(fā)表于 2008-08-27 16:29 |只看該作者
perl -e '$_ = "abcdefg"; s/(.)/$1\n/g; print'

論壇徽章:
0
3 [報告]
發(fā)表于 2008-08-27 16:56 |只看該作者
perl -e '$_ = "how are you 2008";s/ *(\S)/$1\n/g;print'

[ 本帖最后由 caining062 于 2008-8-27 16:58 編輯 ]

論壇徽章:
0
4 [報告]
發(fā)表于 2008-08-27 16:57 |只看該作者
split //, "abcdef"

論壇徽章:
0
5 [報告]
發(fā)表于 2008-08-27 17:08 |只看該作者
unpack   

論壇徽章:
0
6 [報告]
發(fā)表于 2008-08-27 20:22 |只看該作者
多謝樓上諸位。仔細(xì)google了一下,轉(zhuǎn)一篇http://www.perlmeme.org/faqs/man ... ing_characters.html的文章總結(jié)一下
Solution 1: split

If you pass no seperator into split, it will split on every character:

   
  1. #!/usr/bin/perl
  2.     use strict;
  3.     use warnings;

  4.     my $string = "Hello, how are you?";

  5.     my @chars = split("", $string);

  6.     print "First character: $chars[0]\n";
復(fù)制代碼


The output of this program is:

    First character: H

Solution 2: substr

You can access an individual character using substr:

   
  1. #!/usr/bin/perl
  2.     use strict;
  3.     use warnings;

  4.     my $string = "Hello, how are you?";
  5.     my $char = substr($string, 7, 1);

  6.     print "Char is: $char\n";
復(fù)制代碼


The above code would output:

    Char is: h

Solution 3: Unpack

Like substr, if you want a character from a particular position, you can use unpack:

   
  1. #!/usr/bin/perl
  2.     use strict;
  3.     use warnings;

  4.     my $string = "Hello, how are you?";
  5.     my $char = unpack("x9a1", $string);

  6.     print "Char is: $char\n";
復(fù)制代碼


The output of this program is:

    Char is: w

Solution 4: substr and map

If you want to access all the characters individually, you can build them into an array (like Solution 1) using substr and map:

   
  1. #!/usr/bin/perl
  2.     use strict;
  3.     use warnings;

  4.     my $string = "Hello, how are you?";
  5.     my @chars = map substr( $string, $_, 1), 0 .. length($string) -1;

  6.     print "First char is: " . $chars[0] . "\n";

  7.     exit 0;
復(fù)制代碼

The output of this example would be:

    First char is: H

Solution 5: Regular expression

You can use a regular expression to build an array of chars:

   
  1. #!/usr/bin/perl
  2.     use strict;
  3.     use warnings;

  4.     my $string = "Hello, how are you?";
  5.     my @chars = $string =~ /./sg;

  6.     print "Fourth char: " . $chars[3] . "\n";

  7.     exit 0;
復(fù)制代碼


The output of this program is:

    Fourth char: l

Solution 6: unpack

unpack can also unpack individual chars into an array:

  
  1. #!/usr/bin/perl
  2.     use strict;
  3.     use warnings;

  4.     my $string = "Hello, how are you?";
  5.     my @chars = unpack 'a' x length $string, $string;

  6.     print "Eighth char: $chars[7]\n";

  7.     exit 0;
復(fù)制代碼


The output would be:

    Eighth char: h

See also

    perldoc -f split
    perldoc -f substr
    perldoc -f pack
    perldoc -f unpack

論壇徽章:
0
7 [報告]
發(fā)表于 2008-08-27 20:25 |只看該作者
google 得力量是強(qiáng)大得

論壇徽章:
0
8 [報告]
發(fā)表于 2008-08-27 20:25 |只看該作者
there is more than one way to do it
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP