知识大全 TStrings的一些用法

Posted

篇首语:吾生也有涯,而知也无涯。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 TStrings的一些用法相关的知识,希望对你有一定的参考价值。

TStrings的一些用法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  TStrings是一个抽象类 在实际开发中 是除了基本类型外 应用得最多的

  常规的用法大家都知道 现在来讨论它的一些高级的用法

  先把要讨论的几个属性列出来

   CommaText

   Delimiter & DelimitedText

   Names & values & valueFromInd ex

  先看第一个 CommaText 怎么用呢?用代码说话

  const

  constr String = aaa bbb ccc ddd

  var

  strs TStrings

  i Integer

  begin

  strs = TStringList Create

  strs CommaText = constr

  for i = to Strs Count do

  ShowMessage(Strs)

  end

  执行了这段代码后 可以看到ShowMessage显示出来的分别是 aaa bbb ccc ddd

  也就是说 strs CommaText = constr这一句的作用 就是把一个字符串以 为分割符 分段添加到TStrings中

  那么如果不是以 来分割 又该怎么做呢?现在看第二个例子 使用Delimiter和DelimitedText

  const

  constr String = aaa\\bbb\\ccc\\ddd

  var

  strs TStrings

  i Integer

  begin

  strs = TStringList Create

  strs Delimiter = \\

  strs DelimitedText = constr

  for i = to Strs Count do

  ShowMessage(Strs)

  end

  可以看到 显示的效果和第一个例子是一模一样的 解释一下

  Delimiter为分隔符 默认为 DelimitedText就是按Delimiter为分隔符的一个串 得到赋值后回把这个字符串按Delimiter的字符添加到TStrings中

  说到这里 有想起一个属性 QuoteChar 其默认值为 (不包括单引号)

  有何用呢?看例子

  const

  constr String = aaa \\ bbb \\ ccc \\ ddd

  var

  strs TStrings

  i Integer

  begin

  strs = TStringList Create

  strs Delimiter = \\

  strs DelimitedText = constr

  for i = to Strs Count do

  ShowMessage(Strs)

  end

  显示出来的仍然是aaa bbb ccc ddd 为什么不是 aaa bbb ccc ddd 呢?

  再来看一个例子

  const

  constr String = |aaa|\\|bbb|\\|ccc|\\|ddd|

  var

  strs TStrings

  i Integer

  begin

  strs = TStringList Create

  strs Delimiter = \\

  strs QuoteChar = |

  strs DelimitedText = constr

  for i = to Strs Count do

  ShowMessage(Strs)

  end

  显示出来的又是aaa bbb ccc ddd 对比一下 应该不难明白吧?这个就不多说了 用得也不多

  但是还要多说一句 当Delimiter为 而QuoteChar为 时 DelimitedText和CommaText是同等的

  最后要说的三个是 Names & values & valueFromIndex

  看看下面的代码

  const

  constr String = =aaa =bbb =ccc =ddd

  var

  strs TStrings

  i Integer

  begin

  strs = TStringList Create

  strs CommaText = constr

  for i = to strs Count do

  begin

  ShowMessage(strs Names)

  ShowMessage(strs values[strs Names])

  ShowMessage(strs valueFromIndex)

  end

  end

  通过这个例子不难看出

  这个时候strs中的内容是

   =aaa

   =bbb

   =ccc

   =ddd

  而Names中则是

  

  

  

  

  在values中则是

  aaa

  bbb

  ccc

cha138/Article/program/Delphi/201311/8425

相关参考