delphi中字符串拼接

As with any programming language, in Delphi, variables are placeholders used to store values; they have names and data types. The data type of a variable determines how the bits representing those values are stored in the computer's memory.

与任何编程语言一样,在Delphi中 ,变量是用于存储值的占位符; 它们具有名称和数据类型。 变量的数据类型决定了代表这些值的位如何存储在计算机的内存中。

When we have a variable that will contain some array of characters, we can declare it to be of typeString. Delphi provides a healthy assortment of string operators, functions and procedures. Before assigning a String data type to a variable, we need to thoroughly understand Delphi's four string types.

当我们有一个将包含一些字符数组的变量时,我们可以将其声明为String类型。 Delphi提供了健康的字符串运算符,函数和过程。 在将String数据类型分配给变量之前,我们需要彻底了解Delphi的四种字符串类型。

短弦 ( Short String )

Simply put, Short String is a counted array of (ANSII) characters, with up to 255 characters in the string. The first byte of this array stores the length of the string. Since this was the main string type in Delphi 1 (16 bit Delphi), the only reason to use Short String is for backward compatibility. To create a ShortString type variable we use:

简而言之, 短字符串是(ANSII)字符的计数数组,字符串中最多包含255个字符。 该数组的第一个字节存储字符串的长度。 由于这是Delphi 1(16位Delphi)中的主要字符串类型,因此使用短字符串的唯一原因是为了向后兼容。 要创建ShortString类型的变量,我们使用:


var s: ShortString;
s := 'Delphi Programming';​
//S_Length := Ord(s[0]));
//which is the same as Length(s)

The s variable is a Short string variable capable of holding up to 256 characters, its memory is a statically allocated 256 bytes. Since this is usually wasteful - unlikely will your short string spread to the maximum length - second approach to using Short Strings is using subtypes of ShortString, whose maximum length is anywhere from 0 to 255.

s变量是一个短字符串变量,最多可容纳256个字符,其内存是静态分配的256个字节。 由于这通常很浪费-短字符串不太可能扩展到最大长度-使用短字符串的第二种方法是使用ShortString的子类型,其最大长度在0到255之间。


var ssmall: String[50];
ssmall := 'Short string, up to 50 characters';

This creates a variable called ssmall whose maximum length is 50 characters.

这将创建一个名为ssmall的变量,其最大长度为50个字符。

Note: When we assign a value to a Short String variable, the string is truncated if it exceeds the maximum length for the type. When we pass short strings to some Delphi's string manipulating routine, they are converted to and from long string.

注意:当我们为Short String变量赋值时,如果字符串超过类型的最大长度,则字符串将被截断。 当我们将短字符串传递给某些Delphi的字符串操作例程时,它们将与长字符串进行转换。

弦/长/安西 ( String / Long / Ansi )

Delphi 2 brought to Object Pascal Long String type. Long string (in Delphi's help AnsiString) represents a dynamically allocated string whose maximum length is limited only by available memory. All 32-bit Delphi versions use long strings by default. I recommend using long strings whenever you can.

Delphi 2带来了对象Pascal Long String类型。 长字符串(在Delphi帮助下,AnsiString)表示动态分配的字符串,其最大长度仅受可用内存限制。 默认情况下,所有32位Delphi版本都使用长字符串。 我建议您尽可能使用长字符串。


var s: String;
s := 'The s string can be of any size...';

The s variable can hold from zero to any practical number of characters. The string grows or shrinks as you assign new data to it.

s变量可以容纳从零到任何实际数量的字符。 在为字符串分配新数据时,字符串会增加或缩小。

We can use any string variable as an array of characters, the second character in s has the index 2. The following code

我们可以使用任何字符串变量作为字符数组, s中的第二个字符的索引为2。下面的代码


s[2]:='T';

assigns T to the second character os the s variable. Now the few of the first characters in  look like: TTe s str....Don't be mislead, you can't use s[0] to see the length of the string, s is not ShortString.

T分配给s变量的第二个字符。 现在s中的头几个字符看起来像: TTe s str...。不要误导,您不能使用s [0]来查看字符串的长度, s不是ShortString。

参考计数,写时复制 ( Reference counting, copy-on-write )

Since memory allocation is done by Delphi, we don't have to worry about garbage collection. When working with Long (Ansi) Strings Delphi uses reference counting. This way string copying is actually faster for long strings than for short strings. Reference counting, by example:

由于内存分配是由Delphi完成的,因此我们不必担心垃圾回收。 在使用长(Ansi)字符串时,Delphi使用引用计数。 这样,长字符串的字符串复制实际上比短字符串的复制快。 参考计数,例如:


var s1,s2: String;
s1 := 'first string';
s2 := s1;

When we create string s1 variable, and assign some value to it, Delphi allocates enough memory for the string. When we copy s1 to s2, Delphi does not copy the string value in memory, it only increases the reference count and alters the s2 to point to the same memory location as s1.

当我们创建字符串s1变量并为其分配一些值时,Delphi会为该字符串分配足够的内存。 当我们将s1复制到s2时 ,Delphi不会将字符串值复制到内存中,它只会增加引用计数并更改s2使其指向与s1相同的内存位置。

To minimize copying when we pass strings to routines, Delphi uses copy-on-write technique. Suppose we are to change the value of the s2 string variable; Delphi copies the first string to a new memory location, since the change should affect only s2, not s1, and they are both pointing to the same memory location.

为了在将字符串传递给例程时最大程度地减少复制,Delphi使用了写时复制技术。 假设我们要更改s2字符串变量的值; Delphi将第一个字符串复制到新的存储位置,因为更改仅影响s2,而不影响s1,并且它们都指向相同的存储位置。

宽弦 (  Wide String )

Wide strings are also dynamically allocated and managed, but they don't use reference counting or the copy-on-write semantics. Wide strings consist of 16-bit Unicode characters.

宽字符串也可以动态分配和管理,但是它们不使用引用计数或写时复制语义。 宽字符串由16位Unicode字符组成。

关于Unicode字符集 ( About Unicode character sets )

The ANSI character set used by Windows is a single-byte character set. Unicode stores each character in the character set in 2 bytes instead of 1. Some national languages use ideographic characters, which require more than the 256 characters supported by ANSI. With 16-bit notation we can represent 65,536 different characters. Indexing of multibyte strings is not reliable, since s[i] represents the ith byte (not necessarily the i-th character) in s.

Windows使用的ANSI字符集是一个单字节字符集。 Unicode将字符集中的每个字符存储在2个字节中,而不是1个字节中。某些国家语言使用表意字符,这些字符需要超过ANSI支持的256个字符。 使用16位符号,我们可以表示65,536个不同的字符。 多字节串的索引是不可靠的,因为S [i]表示在S中的第i个字节(不一定是第i个字符)。

If you must use Wide characters, you should declare a string variable to be of the WideString type and your character variable of the WideChar type. If you want to examine a wide string one character at a time, be sure to test for multibite characters. Delphi doesn't support automatic type conversions betwwen Ansi and Wide string types.

如果必须使用Wide字符,则应将字符串变量声明为WideString类型,并将字符变量声明为WideChar类型。 如果要一次检查一个字符的宽字符串,请确保测试多位字符。 Delphi不支持在Ansi和Wide字符串类型之间进行自动类型转换。


var s : WideString;
c : WideChar;
s := 'Delphi_ Guide';
s[8] := 'T';
//s='Delphi_TGuide';

空终止 ( Null terminated )

A null or zero terminated string is an array of characters, indexed by an integer starting from zero. Since the array has no length indicator, Delphi uses the ASCII 0 (NULL; #0) character to mark the boundary of the string. This means there is essentially no difference between a null-terminated string and an array[0..NumberOfChars] of type Char, where the end of the string is marked by #0.

空或零终止的字符串是字符数组,由从零开始的整数索引。 由于数组没有长度指示符,因此Delphi使用ASCII 0(NULL;#0)字符标记字符串的边界。 这意味着以空字符结尾的字符串和Char类型的array [0..NumberOfChars]之间基本上没有区别,其中字符串的末尾由#0标记。

We use null-terminated strings in Delphi when calling Windows API functions. Object Pascal lets us avoid messing arround with pointers to zero-based arrays when handling null-terminated strings by using the PChar type. Think of a PChar as being a pointer to a null-terminated string or to the array that represents one. For more info on pointers, check:Pointers in Delphi.

调用Windows API函数时,我们在Delphi中使用以空值结尾的字符串。 通过使用PChar类型,使用对象Pascal可以避免在处理以零结尾的字符串时,指针指向从零开始的数组。 可以将PChar视为指向以null终止的字符串或表示一个的数组的指针。 有关指针的更多信息,请检查: Delphi中的Pointers 。

For example, The GetDriveType API function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive. The following procedure lists all the drives and their types on a users computer. Place one Button and one Memo component on a form and assign an OnClick handler of a Button:

例如, GetDriveType API函数确定磁盘驱动器是可移动,固定,CD-ROM,RAM磁盘还是网络驱动器。 以下过程列出了用户计算机上的所有驱动器及其类型。 在表单上放置一个Button和一个Memo组件,并为Button分配一个OnClick处理程序:


procedure TForm1.Button1Click(Sender: TObject);
var
Drive: Char;
DriveLetter: String[4];
begin
for Drive := 'A' to 'Z' do
begin
DriveLetter := Drive + ':\';
case GetDriveType(PChar(Drive + ':\')) of
DRIVE_REMOVABLE:
Memo1.Lines.Add(DriveLetter + ' Floppy Drive');
DRIVE_FIXED:
Memo1.Lines.Add(DriveLetter + ' Fixed Drive');
DRIVE_REMOTE:
Memo1.Lines.Add(DriveLetter + ' Network Drive');
DRIVE_CDROM:
Memo1.Lines.Add(DriveLetter + ' CD-ROM Drive');
DRIVE_RAMDISK:
Memo1.Lines.Add(DriveLetter + ' RAM Disk');
end;
end;
end;

混合Delphi的琴弦 ( Mixing Delphi's strings )

We can freely mix all four different kinds of strings, Delphi will give it's best to make sense of what we are trying to do. The assignment s:=p, where s is a string variable and p is a PChar expression, copies a null-terminated string into a long string.

我们可以自由地混合所有四种不同类型的字符串,Delphi会最好地弄清我们正在尝试做的事情。 赋值s:= p,其中s是字符串变量,p是PChar表达式,它将以空值结尾的字符串复制到长字符串中。

角色类型 ( Character types )

In addition to four string data types, Delphi has three character types: CharAnsiChar, and ​WideChar. A string constant of length 1, such as 'T', can denote a character value. The generic character type is Char, which is equivalent to AnsiChar. WideChar values are 16-bit characters ordered according to the Unicode character set. The first 256 Unicode characters correspond to the ANSI characters.

除了四种弦数据类型,德尔福有三种字符类型:CHAR,ANSIChar类型WideChar。 长度为1的字符串常量(例如“ T”)可以表示一个字符值。 通用字符类型为Char,等效于AnsiChar。 WideChar值是根据Unicode字符集排序的16位字符。 前256个Unicode字符对应于ANSI字符。

翻译自: https://www.thoughtco.com/string-types-in-delphi-delphi-for-beginners-4092544

delphi中字符串拼接

delphi中字符串拼接_Delphi中的字符串类型(Delphi适用于初学者)相关推荐

  1. python 字符串拼接_Python中拼接字符串的方法 | 萧小寒

    摘要 在编程语言中,几乎每种语言都有关于字符串的操作方法或函数.原因很简单,字符串做为编程语言中不可或缺的数据类型,有着不可以替代的重要性.不同的是,每种编程语言对于字符串的处理方式都有自己的特色.这 ...

  2. java插入数据库字符串拼接_java中PreparedStatement解决需转义字符向数据库中插入时的转义问题 | 学步园...

    简单的执行如下语句去做数据库的插入操作是有问题的!它处理不了单引号,双引号等需要转义的字符的插入问题! String sql = "insert into emailOriginal(id, ...

  3. Js 字符串拼接使用整理_JavaScript 字符串拼接整理

    一.连接符 (+) 几乎所有语言都支持,必杀技. //语言通用字符串拼接 + let a = '千乐'; let b = '微云'; let result = a + b; console.info( ...

  4. python输出字符串拼接数字_python实现字符串和数字拼接

    python实现字符串和数字拼接 如下所示: 将i前面加str(i)就可以了 补充拓展:python 连接字符串和数字 python 连接字符串和数字的问题: 首先要说的是,python是强类型的语言 ...

  5. deliphi 字符串分割_delphi中拆分字符串的函数

    delphi的字符截取函数LeftStr, MidStr, RightStr拆分字符串 这几个函数都包含在StrUtils中,所以需要uses StrUtils; 假设字符串是 Dstr := 'De ...

  6. Objective-C 字符串拼接函数 多个不同类型的参数拼接到一个字符串 类似于Java中 String.format()方法的原生API

    总目录 iOS开发笔记目录 从一无所知到入门 文章目录 需求 Screenshot Code Output 需求 我有多个参数(类型也许不同),需要拼接到一个字符串中. 在Java中有String.f ...

  7. deliphi 字符串分割_Delphi中 分割字符串(两种方法)

    对一个有分隔符的字符串如何分割成几个单一小字符串 1.使用CommaText Var Stl:TStringList; Const S:='20071018085500   155.30  220.6 ...

  8. php中怎么拼接,php中字符串的拼接用法详解

    首先和大家说下,学习任何一门语言都要去官网去看文档,因为官方的文档正确性有保证,并且也最有广泛性. 有两个字符串(string)运算符.第一个是连接运算符("."),它返回其左右参 ...

  9. delphi 复制到剪贴板_Delphi中的基本剪贴板操作(剪切/复制/粘贴)

    delphi 复制到剪贴板 The Windows Clipboard represents the container for any text or graphics that are cut, ...

最新文章

  1. 计算机界最大“追书坑”,82岁大神高德纳仍在写《计算机程序设计艺术》4B卷...
  2. 八皇后时间复杂度_回溯算法 | 追忆那些年曾难倒我们的八皇后问题
  3. 一道很简单的贪心算法题~【贪心:我不要脸的伐?】
  4. ionic3-android打包完美解决
  5. 【Vue】—解构插槽 Prop以及具名插槽的缩写
  6. 基于ggplot2网络可视化(二)
  7. 微擎支持html微信支付,微信小程序云开发:现已原生支持微信支付
  8. maven报错JAVA_HOME should point to a JDK not a JRE
  9. Robo 3T(Studio 3T)最新版安装教程
  10. QOpenGLWight与QPainter混合渲染
  11. TFS 2010 使用手册(四)备份与恢复
  12. Laravel5.6 模块化公众号与小程序系统项目实战
  13. 如何解决u盘不能拷贝超过4G的大文件
  14. 微信小程序实现页面按钮分享,右上角三个点分享禁用分享
  15. Python face_recognition 库人脸识别/匹配教程
  16. 计算机培训后的感言,计算机中级培训感言
  17. 深度理解感受野(一)什么是感受野?
  18. 听说你还在纠结自己没访问量?成不了“博客专家”?
  19. 微信网页开发wx.getLocation在安卓手机上的一个坑
  20. 【嵌入式应用4】ROS话题编程

热门文章

  1. 开源素材网_34个用于广告素材的开源工具
  2. 在线查看word文档
  3. 坚守天门 1.0版 发布
  4. github如何pull_request
  5. html dom反选,Dom操作--全选反选
  6. 【雨丝纷纷忆故人无标题】
  7. 又一国产开源项目走向世界,百度RPC框架Apache bRPC正式成为ASF顶级项目
  8. Cesium隐藏地球底图
  9. PNAS:快速脑电波振荡识别并定位癫痫患者的大脑
  10. [Chaosblade] chaosblade-jvm-exec和chaosblade交互