[Erlang 0107] Erlang实现文本截断
> string:substr("abcd我们就是喜欢Erlang,就是喜欢OTP",,).
[,,]
> io:format("~ts",[v()]).
d我们ok
Eshell V5.10.2 (abort with ^G)
> u:sub().
[,,]
> io:format("~ts",[v()]).
dæok
> q().
ok
>
%% coding: utf-
Eshell V5.10.2 (abort with ^G)
> u:sub().
[,,]
> io:format("~ts",[v()]).
d我们ok
-define(DEFAULT_ENCODING, latin1). -spec default_encoding() -> source_encoding(). default_encoding() ->
?DEFAULT_ENCODING. -spec encoding_to_string(Encoding) -> string() when
Encoding :: source_encoding(). encoding_to_string(latin1) -> "coding: latin-1";
encoding_to_string(utf8) -> "coding: utf-8".
The Erlang source file encoding is selected by a comment in one of the first two lines of the source file. The first string that matches the regular expression coding\s*[:=]\s*([-a-zA-Z0-9])+ selects the encoding. If the matching string is not a valid encoding it is ignored. The valid encodings are Latin-1 and UTF-8 where the case of the characters can be chosen freely.
As of Erlang/OTP R16 Erlang source files can be written in either UTF-8 or bytewise encoding (a.k.a. latin1 encoding). The details on how to state the encoding of an Erlang source file can be found in epp(3). Strings and comments can be written using Unicode, but functions still have to be named using characters from the ISO-latin-1 character set and atoms are restricted to the same ISO-latin-1 range. These restrictions in the language are of course independent of the encoding of the source file. Erlang/OTP R18 is expected to handle functions named in Unicode as well as Unicode atoms. http://www.erlang.org/doc/apps/stdlib/unicode_usage.html
R16B之前版本
-module(u).
-compile(export_all).
test() ->
t("abcd我们就是喜欢Erlang,就是喜欢OTP",). test2() ->
tw("Youth is not a time of life; it is a state of mind; it is not a matter of
rosy cheeks, red lips and supple knees; it is a matter of the will, a
quality of the imagination, a vigor of the emotions; it is the freshness of
the deep springs of life.",10). dump(FileName,Data)->
file:write_file(FileName, io_lib:fwrite("~s.\n", [Data])). sub()->
string:substr("abcd我们就是喜欢Erlang,就是喜欢OTP",,). t(Input,Max) ->
truncatechars(Input,Max). tw(Input,Max) ->
truncatewords(Input,Max). %% @doc Truncates a string after a certain number of characters.
truncatechars(_Input, Max) when Max =< ->
"";
truncatechars(Input, Max) when is_binary(Input) ->
list_to_binary(truncatechars(binary_to_list(Input), Max));
truncatechars(Input, Max) ->
truncatechars(Input, Max, []). %% @doc Truncates a string after a certain number of words.
truncatewords(_Input, Max) when Max =< ->
"";
truncatewords(Input, Max) when is_binary(Input) ->
list_to_binary(truncatewords(binary_to_list(Input), Max));
truncatewords(Input, Max) ->
truncatewords(Input, Max, []). truncatechars([], _CharsLeft, Acc) ->
lists:reverse(Acc);
truncatechars(_Input, , Acc) ->
lists:reverse("..." ++ Acc);
truncatechars([C|Rest], CharsLeft, Acc) when C >= # ->
truncatechars(Rest, CharsLeft + , [C|Acc]);
truncatechars([C|Rest], CharsLeft, Acc) when C >= # ->
truncatechars(Rest, CharsLeft + , [C|Acc]);
truncatechars([C|Rest], CharsLeft, Acc) when C >= # ->
truncatechars(Rest, CharsLeft + , [C|Acc]);
truncatechars([C|Rest], CharsLeft, Acc) when C >= # ->
truncatechars(Rest, CharsLeft + , [C|Acc]);
truncatechars([C|Rest], CharsLeft, Acc) when C >= # ->
truncatechars(Rest, CharsLeft, [C|Acc]);
truncatechars([C|Rest], CharsLeft, Acc) ->
truncatechars(Rest, CharsLeft - , [C|Acc]). truncatewords(Value, _WordsLeft, _Acc) when is_atom(Value) ->
Value;
truncatewords([], _WordsLeft, Acc) ->
lists:reverse(Acc);
truncatewords(_Input, , Acc) ->
lists:reverse("..." ++ Acc);
truncatewords([C1, C2|Rest], WordsLeft, Acc) when C1 =/= $\ andalso C2 =:= $\ ->
truncatewords([C2|Rest], WordsLeft - , [C1|Acc]);
truncatewords([C1|Rest], WordsLeft, Acc) ->
truncatewords(Rest, WordsLeft, [C1|Acc]).
test() ->
t("abcd我们就是喜欢Erlang,就是喜欢OTP",). dump(FileName,Data)->
file:write_file(FileName, io_lib:fwrite("~s.\n", [Data])). Eshell V5.10.2 (abort with ^G)
> u:test().
[,,,,,,,,,,,,,,,
,,,,,,,,,]
>
> u:dump("u_result",v()).
ok
>
[root@nimbus demo]# cat u_result
abcd我们就是喜欢....
|
Unicode编码(16进制)
|
UTF-8 字节流模板
|
|
000000 - 00007F
|
0xxxxxxx
|
|
000080 - 0007FF
|
110xxxxx 10xxxxxx
|
|
000800 - 00FFFF
|
1110xxxx 10xxxxxx 10xxxxxx
|
|
010000 - 10FFFF
|
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
Eshell V5.10.2 (abort with ^G)
> unicode:characters_to_binary("开心").
<<,,,,,>>
> unicode:characters_to_list("开心").
[,]
> integer_to_list(,).
""
> integer_to_list(,).
""
> integer_to_list(,).
""

[Erlang 0107] Erlang实现文本截断的更多相关文章
- [Erlang 0124] Erlang Unicode 两三事 - 补遗
最近看了Erlang User Conference 2013上patrik分享的BRING UNICODE TO ERLANG!视频,这个分享很好的梳理了Erlang Unicode相关的问题,基本 ...
- [Erlang 0129] Erlang 杂记 VI
把之前阅读资料的时候记下的东西,整理了一下. Adding special-purpose processor support to the Erlang VM P23 简单介绍了Erlang C ...
- [Erlang 0122] Erlang Resources 2014年1月~6月资讯合集
虽然忙,有些事还是要抽时间做; Erlang Resources 小站 2014年1月~6月资讯合集,方便检索. 小站地址: http://site.douban.com/204209/ ...
- [Erlang 0105] Erlang Resources 小站 2013年1月~6月资讯合集
很多事情要做,一件一件来; Erlang Resources 小站 2013年1月~6月资讯合集,方便检索. 小站地址: http://site.douban.com/204209/ ...
- Erlang 103 Erlang分布式编程
Outline 笔记系列 Erlang环境和顺序编程Erlang并发编程Erlang分布式编程YawsErlang/OTP 日期 变更说明 2014-11-23 A Outl ...
- 解决NSTextContainer分页时文本截断问题
解决NSTextContainer分页时文本截断问题 NSTextContainer与NSLayoutManager配合使用可以将大文本文件分页,但是,分页过程中会遇到问题,显示字符被截断的问题:) ...
- [Erlang 0057] Erlang 排错利器: Erlang Crash Dump Viewer
http://www.cnblogs.com/me-sa/archive/2012/04/28/2475556.html Erlang Crash Dump Viewer真的是排错的天兵神器,还记得我 ...
- [Erlang 0119] Erlang OTP 源码阅读指引
上周Erlang讨论群里面提到lists的++实现,争论大多基于猜测,其实打开代码看一下就都明了.贴出代码截图后有同学问这代码是哪里找的? "代码去哪里找?",关于Erla ...
- [Erlang 0123] Erlang EPMD
epmd进程和Erlang节点进程如影随形,在Rabbitmq集群,Ejabberd集群,Couchbase集群产品文档中都会有相当多的内容讲epmd,epmd是什么呢? epmd 是Erlan ...
随机推荐
- ZOJ Problem Set - 1045 HangOver
#include <stdio.h> int main() { float c; int i; while(scanf("%f",&c)!=EOF&&a ...
- JavaMail发送邮件的笔记及Demo
最近碰到一个需求,就是注册用户时候需要向用户发送激活邮箱,于是照着网上搜来的demo自己试着运行了一下,发件时我用的是网易163邮箱,收件时用QQ邮箱,运行后报了一个错误: 网络上搜索解决方式,多次尝 ...
- 前端开发编辑器(notepad++、sublime text)
1.Notepad++ 正则替换: 如<td>第三节</td> 替换成<td><input type="text" value=" ...
- web前端学习随笔
为什么是随笔 好好算下来,学习web前端已有半个月了,这半个月来主要学习的是HTML和CSS部分,期间有困惑,也有解决困惑时的快感,所以想把这段时间感受到的一些东西记下来,因为内容比较杂,所以干脆叫随 ...
- TCP三次握手和四次挥手状态变迁解析
TCP是TCP/IP的传输层控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 首先需要了解几个名词:tcp标志位,有6种分别为:SYN(synchronous建立联机) .ACK(ackn ...
- VARCHAR列上的索引
一年前,我写了在索引的导航结构里,SQL Server如何存储VARCHAR列.我们都知道,在SQL Server里索引(聚集索引,非聚集索引)的键列有最大900byte的大小限制. 假设现在你想捉弄 ...
- 分享一个基于HTML5实现的视频播放器
什么是hivideo? 最近一段时间在使用PhoneGap开发一个App应用,App需要播放视频,本想直接使用html5的video,但使用它在全屏播放时不支持横屏播放,只能放弃.最终决定还是自己封装 ...
- 使用VS Code开发 调试.NET Core 应用程序
使用VS Code开发 调试.NET Core RC2应用程序,由于.NET Core 目前还处于预览版. 本文使用微软提供的示例进行开发及调试. https://github.com/aspnet/ ...
- jQuery全屏动画焦点图
效果:http://hovertree.com/texiao/jqimg/3/ 本效果使用 jquery-1.8.3.min.js,如需使用1.12.3版本,需进行修改. 全部版本jQuery下载:h ...
- SQL Server帐号孤立的问题解决
网站近日经常遭到攻击,好几次数据库挂马,前几天把论坛升级了,今天又升级了数据库,把之前的MSSQL 2000 升级到MSSQL 2005,用的是数据库还原功能还原的,遇到了这个帐号孤立的问题. 什么是 ...
