惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
爱范儿
爱范儿
Jina AI
Jina AI
博客园 - Franky
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
M
MIT News - Artificial intelligence
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
腾讯CDC
博客园_首页
月光博客
月光博客
有赞技术团队
有赞技术团队
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
MyScale Blog
MyScale Blog

博客园 - Pandora

MongoDB on Asp.Net MVC3–CRUD MongoDB on Asp.Net MVC3 [WebGL] 简介,流程及示例 2010 Web前端技术趋势及总结 转载 - 杂念0828 【F#2.0系列】定义递归函数 【F#2.0系列】使用选项(Option) 【F#2.0系列】使用F#的List 【F#2.0系列】使用F#进行算术操作 【F#2.0系列】F#调用.NET 类库 【F#2.0系列】目录 【F#2.0系列】F#入门(2) 【F#2.0系列】F#入门(1) 【F#2.0系列】概述 活该如此 Visual Studio 2010 XAML Editor IntelliSense Extension Microsoft 发布了第一个IE9 developer preview 支持HTML5硬件加速 ASP.NET 4.0 来了 []()+! 实现JavaScript代码的原理
【F#2.0系列】介绍String类型 - Pandora - 博客园
Pandora · 2010-08-27 · via 博客园 - Pandora

F#string类型是.NETSystem.String的缩写形式,代表了一连串的Unicode UTF-16字符。

使用String

几种不同的String书写方式:

示例

种类

类型

"Humpty Dumpty"

字符串

string

"c:\\Program Files"

字符串

string

@"c:\Program Files"

无转义(Verbatim) string

string

"xyZy3d2"B

Literal byte array

byte []

'c'

字符

char

转义字符:

字符

含义

ASCII/Unicode

示例

\n

换行

10

"\n"

\r

回车

13

"\r"

\t

Tab

9

"\t"

\b

Backspace

8

\NNN

使用三位数字表示的字符

NNN

"\032" (space)

\uNNNN

Unicode 字符

NNNN

"\u00a9" (©)

\UNNNNNNNN

Long Unicode 字符

NNNN NNNN

"\U00002260"(_)

Byte array中的字符都是ASCII字符。非ASCII字符需要使用转义符。

将一个字符串写为两行:

> let s = "All the kings horses
- and all the kings men";;

支持通过.[]来访问字符串的特定字符:

> let s = "Couldn't put Humpty";;
val s : string
 
> s.Length;;
val it : int = 19
 
> s.[13];;

使用.[index..index]可以获取子字符串(substring)

> let s = "Couldn't put Humpty";;
val s : string
 
> s.[13..16];;

字符串是不可变的(immutable);就是说,一个字符串的值在其生成之后就不能被改变。例如:Substring方法并不会修改原字符串本身,而是返回了一个新的字符串。

当你视图修改一个字符串的时候,你会得到一个error

> let s = "Couldn't put Humpty";;
val s : string = "Couldn't put Humpty"
 
> s.[13] <- 'h';;
 
  s.[13] <- 'h';;
  ^^
stdin(75,0): error: FS0001: Type error in the use of the overloaded operator
'set_Item'. The type 'string' does not support any operators named 'set_Item'

构造一个字符串

最简单的方式就是使用+操作符:

> "Couldn't put Humpty" + " " + "together again";;
val it : string = "Couldn't put Humpty together again"

我们依然可以使用System.Text.StringBuilder来构建:

> let buf = new System.Text.StringBuilder();;
val buf : System.Text.StringBuilder
 
> buf.Append("Humpty Dumpty");;
 
> buf.Append(" sat on the wall");;
 
> buf.ToString();;
val it : string = "Humpty Dumpty sat on the wall"

同时,F#兼容OCaml^操作符。(感觉上和+是一回事。)

 目录传送门