String and String[]

What will be the advantage of using
myString: String[10]
over
myString: String
0
Man
11/15/2012 12:39:43 AM
embarcadero.delphi.win32 2183 articles. 0 followers. Follow

31 Replies
1888 Views

Similar Articles

[PageSpeed] 4

Man wrote:

> What will be the advantage of using
> myString: String[10]
> over
> myString: String

"String[10]" is a fixed-length, statically allocated, Ansi-based ShortString.

"String" is a dynamic-length, dynamically allocated, reference counted, Ansi-based 
or Unicode-based (depening on Delphi version) Long string.

Unless you absolutely need the static, fixed length nature of ShortString, 
such as in records that interact with external systems, you should stay away 
from ShortString in modern code.

--
Remy Lebeau (TeamB)
0
Remy
11/15/2012 1:04:05 AM
Man Fan wrote:

> What will be the advantage of using
> myString: String[10]
> over
> myString: String

No advantage whatsoever. Don't use the [] form anymore. It is a remnant
of Turbo Pascal and should not be used in new code anymore.

The [] form will create a so called short string of a certain length
(10 characters plus length byte, in this case). It will not contain
Unicode, only plain Ansi.

-- 
Rudy Velthuis (TeamB)    http://www.teamb.com

"if you keep doing what you've always done, you'll keep getting
 what you've always gotten."
 -- Jim Rohn
0
Rudy
12/1/2012 1:56:06 AM
> {quote:title=Man Fan wrote:}{quote}
> What will be the advantage of using
> myString: String[10]
> over
> myString: String

Fast access to write character values without using of pointers.
Some code examples are here

http://guildalfa.ru/alsha/node/26

--
Aleksandr
0
Aleksandr
12/1/2012 6:45:11 AM
> Fast access to write character values without using of pointers.
> Some code examples are here
> 
> http://guildalfa.ru/alsha/node/26

The code for a Delphi rather than a TP-style string needs just the AnsiChar cast changed to a Char one. Internally the SetString call will involve a dynamic memory allocation of course, but I'm struggling to see how that's going to be an issue in practice (and if it is, just preallocate up front and pass it in as a var parameter). Or are you seriously suggesting P^[Index] is a performance killer relative to S[Index], where P is a dynamically allocated array (like a long string) and S is a statically alloc
ated one (like a short string)?
0
Chris
12/1/2012 9:25:08 AM
> {quote:title=Chris Rolliston wrote:}
> Or are you seriously suggesting... 

Where?

You think so because of my English may be.
The suggestion consists that if fast writing of string bytes is necessary and you don't want to work with pointers the only way is to use short strings.

Edited by: Aleksandr Sharahov on Dec 1, 2012 1:50 PM
0
Aleksandr
12/1/2012 10:52:56 AM
> You think so because of my English may be.

Your English is fine - it's your technical claim I don't understand.

> The suggestion consists that if fast writing of string bytes is necessary and you don't want to work with pointers the only way is to use short strings.

In your benchmarks, where does using short strings outperform long strings? There's nothing inherent in the code you linked to that necessitates using ShortString rather than string - far from it, it looks like long string code that just happens to type its string variable to ShortString (I say this because SetString did not exist before long strings were introduced).
0
Chris
12/1/2012 7:48:05 PM
Let's consider assignment of one character of long string. There is overcall of UniqueString in asm-code which compiler generates for such assignment. And there are millions of assignments in my functions.

Well-known workaround is the using ponters, for example http://guildalfa.ru/alsha/node/17
0
Aleksandr
12/1/2012 8:33:45 PM
> {quote:title=Aleksandr Sharahov wrote:}{quote}
> Let's consider assignment of one character of long string. There is overcall of UniqueString in asm-code which compiler generates for such assignment.

Oh right, gotcha. I just cast to a PChar at the start myself. Not sure why this is more difficult to understand...?

{code}
var
  SeekPtr: PChar;
begin
  SetString(Result, 12);
  SeekPtr := PChar(Result);
  //...
{code}

> Well-known workaround is the using ponters, for example http://guildalfa.ru/alsha/node/17

Using a Pointer cast is a bit OTT in my view. A PChar one only adds a check for nil (returning a pointer to a null terminator if so), and that's desirable behaviour when receiving the data as a parameter (you can start deferencing the PChar without thinking about it). Moreover, a Pointer cast stops the compiler being able to help you when writing code that crosses the Unicode divide:

{code}
var
  S: string;
  SeekPtr: PAnsiChar;
begin
  S := 'Blah blah blah';
  SeekPtr := Pointer(S);   //compiles with no warning
  SeekPtr := PAnsiChar(S); //W1044 Suspicious typecast of string to PAnsiChar
 {code}
0
Chris
12/2/2012 10:27:29 AM
That's not the point. The point is the statements like 

{code}s[i]:=s[j];{code}
0
Aleksandr
12/2/2012 10:57:23 AM
Aleksandr Sharahov wrote:

> Let's consider assignment of one character of long string. There is
> overcall of UniqueString in asm-code which compiler generates for
> such assignment.

Which is a nop in most cases.

-- 
Rudy Velthuis (TeamB)    http://www.teamb.com

"I'd rather be a climbing ape than a falling angel."
 -- Terry Pratchett
0
Rudy
12/2/2012 2:32:34 PM
> {quote:title=Rudy Velthuis (TeamB) wrote:}{quote}
> Aleksandr Sharahov wrote:
> 
> > Let's consider assignment of one character of long string. There is
> > overcall of UniqueString in asm-code which compiler generates for
> > such assignment.
> 
> Which is a nop in most cases.


It must be very big NOP if it makes function 2-4 times slower.

--
Aleksandr
http://guildalfa.ru/alsha
0
Aleksandr
12/2/2012 3:36:00 PM
Aleksandr Sharahov wrote:

> > {quote:title=Rudy Velthuis (TeamB) wrote:}{quote}
> > Aleksandr Sharahov wrote:
> > 
> > > Let's consider assignment of one character of long string. There
> > > is overcall of UniqueString in asm-code which compiler generates
> > > for such assignment.
> > 
> > Which is a nop in most cases.
> 
> It must be very big NOP if it makes function 2-4 times slower.

Indeed. I'll have to check this.

-- 
Rudy Velthuis (TeamB)    http://www.teamb.com

"I can envision a small cottage somewhere, with a lot of
 writing paper, and a dog, and a fireplace and maybe enough
 money to give myself some Irish coffee now and then and
 entertain my two friends."
 -- Richard Van de Geer, letter, 1975, last American to die in
 Vietnam War, Time, 1985
0
Rudy
12/2/2012 3:53:47 PM
Rudy,

| Don't use the [] form anymore. It is a remnant
| of Turbo Pascal and should not be used in new code anymore.

Bullpuckey!!!

|  It will not contain Unicode, only plain Ansi.

Which is an ENTIRELY VALID reason TO USE string[10]!!!  


-- 

   Q 

12/02/2012 09:28:27

1.19.1.372  [Q'sBrokenToolBar] [Running on TQ]
0
Quentin
12/2/2012 5:30:13 PM
Quentin Correll wrote:

> Bullpuckey!!!
> 
> >   It will not contain Unicode, only plain Ansi.
> 
> Which is an ENTIRELY VALID reason TO USE string[10]!!! 

Q, you're mellowing. ;-)

Bill
0
William
12/2/2012 6:20:21 PM
> That's not the point. The point is the statements like 
> 
> {code}s[i]:=s[j];{code}

Er, it is *a* point - you were implying as exemplary the code you linked to, and I'm saying it isn't for the reason I gave.

I still don't understand your ShortString advocacy - if someone can grasp the UniqueString point (and equally, have it actually matter for them in practice), then they can grasp the concept of PChar (or should be able to).
0
Chris
12/2/2012 7:52:29 PM
Quentin Correll wrote:

> Rudy,
> 
> >  Don't use the [] form anymore. It is a remnant
> >  of Turbo Pascal and should not be used in new code anymore.
> 
> Bullpuckey!!!

You can call it whatever you like, fact is that it is an extremely bad
idea to use them in new code, as they have been deprecated since 1997
or so. It is something left over and totalyl obsolete from 16 bit
Windows days. It is also very likely that they will be removed from any
new compilers, along with language features like old-style TP
"object"s, "absolute", etc.
 
> >   It will not contain Unicode, only plain Ansi.
> 
> Which is an ENTIRELY VALID reason TO USE string[10]!!!  

No, it isn't. It is perhaps *conventient* to use string[10], but the
type is deprecated, and that says it all. Don't use deprecated types,
if you value the maintainability of your code. The code is indeed
"valid" (meaning "it will compile", but so will old-style "object"s and
"absolute"), but it is an etremely bad idea to use the type in new code.

-- 
Rudy Velthuis (TeamB)    http://www.teamb.com

"Freedom in general may be defined as the absence of obstacles 
 to the realization of desires." -- Bertrand Russell
0
Rudy
12/2/2012 8:08:08 PM
William,

| Q, you're mellowing. ;-)

I'm just getting tired of this idea.  <g> 


-- 

   Q 

12/02/2012 13:50:47

1.19.1.372  [Q'sBrokenToolBar] [Running on TQ]
0
Quentin
12/2/2012 9:51:15 PM
Quentin Correll wrote:

> I'm just getting tired of this idea.  <g> 

Yours, or Rudy's?

Bill
0
William
12/3/2012 9:34:25 PM
> Chris Rolliston wrote: 
> I still don't understand your ShortString advocacy...

I try to answer a question of the topic starter "What will be the advantage of using ShortString?"
0
Aleksandr
12/4/2012 9:23:01 AM
William,

| |  I'm just getting tired of this idea.  <g> 
| 
| Yours, or Rudy's?

As if you didn't know.  ;-) 


-- 

   Q 

12/04/2012 10:23:51

1.19.1.372  [Q'sBrokenToolBar] [Running on TQ]
0
Quentin
12/4/2012 6:24:06 PM
Quentin Correll wrote:

> As if you didn't know.  ;-) 

Me???

Bill
0
William
12/4/2012 6:28:30 PM
William,

| |  As if you didn't know.  ;-) 
| 
| Me???

You!!! 


-- 

   Q 

12/04/2012 12:24:47

1.19.1.372  [Q'sBrokenToolBar] [Running on TQ]
0
Quentin
12/4/2012 8:25:00 PM
Quentin Correll wrote:

> You!!!

LOL

Bill
0
William
12/4/2012 8:34:13 PM
Aleksandr Sharahov wrote:

> > Chris Rolliston wrote: 
> > I still don't understand your ShortString advocacy...
> 
> I try to answer a question of the topic starter "What will be the
> advantage of using ShortString?"

The only advantage is that you can save it to a file (or read it from a
file) directly, without extra processing.

The big disadvantages are that it doesn't carry Unicode(UTF-16), that
it is very short (max. 255 characters), that most processing requires
conversion to a normal string (type "string") and that you probably
won't be able to use ShortStrings for very long anymore.

-- 
Rudy Velthuis (TeamB)    http://www.teamb.com

"Real Programmers always confuse Christmas and Halloween because
 Oct31 == Dec25 !" -- Andrew Rutherford
0
Rudy
12/4/2012 10:52:28 PM
> Rudy Velthuis (TeamB) wrote
> 
> The only advantage is that you can save it to a file (or read it from a
> file) directly, without extra processing.

The disadvantage is that you have to write holes to the file. I vote for extra processing.

> The big disadvantages are that it doesn't carry Unicode(UTF-16), 

There is the whole world of applications to which Unicode isn't necessary.

> that it is very short (max. 255 characters),

And disadvantage of the byte is that it has only 8 bits )
 
> that most processing requires conversion to a norma l string (type "string") 

It depends on

> and that you probably won't be able to use ShortStrings for very long anymore.

Why? )

--
Aleksandr
http://guildalfa.ru/alsha/
0
Aleksandr
12/5/2012 10:15:49 AM
Aleksandr Sharahov wrote:

> > Rudy Velthuis (TeamB) wrote
> > 
> > The only advantage is that you can save it to a file (or read it
> > from a file) directly, without extra processing.
> 
> The disadvantage is that you have to write holes to the file. 

The advanatage is that records are all the same size, so it is quite
simple to do random access, instead of just sequential.

But, as Ir said elsewhere, an array[0..n] of AnsiChar or Byte will
achieve the same and works without ShortString.

> > and that you probably won't be able to use ShortStrings for very
long anymore.

> Why? )

I've explained that in a few other threads already. Just search for my
name and ShortString.
-- 
Rudy Velthuis (TeamB)    http://www.teamb.com

"Beliefs are what divide people. Doubt unites them."
 -- Peter Ustinov
0
Rudy
12/5/2012 7:50:16 PM
> Rudy Velthuis (TeamB) wrote:
> 
> The advanatage is that records are all the same size, so it is quite
> simple to do random access, instead of just sequential.

Random access to the block of strings allows strings of variable length.

--
Aleksandr
http://guildalfa.ru/alsha/

"All that glitters is not gold."
0
Aleksandr
12/5/2012 9:36:15 PM
Aleksandr Sharahov wrote:

> > Rudy Velthuis (TeamB) wrote:
> > 
> > The advanatage is that records are all the same size, so it is quite
> > simple to do random access, instead of just sequential.
> 
> Random access to the block of strings allows strings of variable
> length.

In a file? With a fixed size record, you just 

  SomeStream.Seek(SizeOf(theRecord) * Index, poFromBeginning);

and then read or write the record. For such records, ShortString would
be useful, if it were not deprecated. I have no idea what you do with
variable length strings in records.
-- 
Rudy Velthuis (TeamB)    http://www.teamb.com

Finagle's Laws:

   1. Once a job is fouled up, anything done to improve it makes 
      it worse.
   2. No matter what results are expected, someone is always 
      willing to fake it.
   3. No matter what the result, someone is always eager to 
      misinterpret it.
   4. No matter what occurs, someone believes it happened 
      according to his pet theory.
0
Rudy
12/6/2012 11:04:59 AM
> Rudy Velthuis (TeamB) wrote:
> 
> I have no idea what you do with
> variable length strings in records.

At first we calculate a hash of data, then we write data on the calculated page of the file.

--
Aleksandr
http://guildalfa.ru/alsha/

"Even if your explanation is so clear, that excludes any false interpretation, 
all the same there will be a person who will misunderstand you."
0
Aleksandr
12/6/2012 12:45:00 PM
Aleksandr Sharahov wrote:

> > Rudy Velthuis (TeamB) wrote:
> > 
> > I have no idea what you do with
> > variable length strings in records.
> 
> At first we calculate a hash of data, then we write data on the
> calculated page of the file.

Sounds a little less straightforward than just saving to or reading
everything at once from a simple offset. That is the main reason people
use ShortString, AFAICT. For everything else, normal strings are better
suited.

-- 
Rudy Velthuis (TeamB)    http://www.teamb.com

"He had decided to live forever or die in the attempt."
 -- Joseph Heller.
0
Rudy
12/6/2012 11:18:26 PM
> Rudy Velthuis (TeamB) wrote:
> 
> That is the main reason people use ShortString, AFAICT.

Agreed. It is especially right for beginners. 
However in the advanced applications and databases similar methods are used.

--
Aleksandr
http://guildalfa.ru/alsha/

"Program complexity grows until it exceeds the capabilities of the programmer who must maintain it."
0
Aleksandr
12/7/2012 9:16:12 AM
Reply:

Similar Artilces:

string to string[]
Hi, I've got this code : string[] Params; string SQL = "SELECT * FROM T_MANAGEMENT_PAGES"; SqlCommand myCommand = new SqlCommand(SQL, myConnection); myConnection.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); try { while (myReader.Read()) { Params_Type = myReader.GetValue(0).ToString(); } } catch { } finally { } myReader.Close(); My problem is to obtain Params_Type. But each time, it says : "impossible to convert '[object]' in 'string[]' &quo...

string() = string()
this code does not work (not instance of object) Dim Items(), Other() As String Items = New String() {"A","B","C"} Other = New String() {} Other = Items how can I get all the values ? angiras Hi, If I understand your requirement correctly, this should do it Dim Items() As String = {"A", "B", "C"} Dim Other(Items.Length - 1) As String Items.CopyTo(Other, 0) To more closely match you code above, but this will be less efficient Dim Items(), Other() As String ...

String and string?
Hi guys, I was wondering the difference between small letter string type and a capital letter String type. Does anyone know about this? Highly appreciate for your answers! Thankz! The "proper" version is String, as it references the System.String class of the .NET Framework Class Library. The ability to use string is provided by C#, where it is an alias to String. Similarly, the "proper" version of an integer is Int32, as it references System.Int32.  The ability to use int is provided by C#, and the ability to use Integer is provided by VB.NET, and both int and Integer are aliases...

string and string[]
hello, whats the difference here between string and string[] thanks "string" denotes an item of type String "string[]" denotes an array of type String itemsThanks, EdMicrosoft MVP - ASP/ASP.NET Gracia mon ami...

a string in a string in a string in my code behind....
I know my title talk about code-behind, but this is really a client-side problem (and surely a newby one too, sorry about that).I have a javascript function that my make a little text appear when the onmouseover event of a link is activated.So, normally, if my link was inside my html code, it would be something like this:<a href = "whocare.com"  onmouseover="poplink('I want to test something');">test</a>BUT, for some reason, I have to write this part in code behind with the LiteralControl function....and that will give something like that:this.Controls.Add(new LiteralCo...

m_AVP As IDictionary(Of String, String) = New Dictionary(Of String, String) ,not defined?
   I am creating a usercontrol (ascx,ascx.vb)   I have used Imports system.Collections Imports system.Collections.Generic     Private m_AttributeValuePair As IDictionary(Of String, String) = New Dictionary(Of String, String)     But I am gettting an error “Type Dictionary is not defined”   And also I am getting intelisense to type the line But what is the reason for my error ? How can I get escape from the error ?         this error is not showing today  ...

How to Encrypt a String to a String?
Hi Folks,I’m using Triple DES to encrypt a string… the output of the encryption is a byte array.However, I want to store the encrypted result as a string in a SQL Database and not as a byte array.(The reason I want to store it as a string, is to be able to copy & paste the result to a standalone windows form application  to decrypt it...) I’ve tried taking the byte array, converting it into a string…And then reconverting it into a byte array for the decryption. This causes an error upon decryption…How can I convert the encrypted byte array into a string and back into a byt...

RSA Encryption
Here is my goal:1. Take a string2. Encrypt it 3. Pass it as a parameter in the QueryString4. Decrypt it The value starts as a string, then is converted to a byte[] and then encrypted.  The resulting byte[] is converted to a string and send as a parameter.  The recieving page decrypts the string (creates a byte[], decrypts to a new byte[], and the value is finally parsed for its values) I am using http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyrsacryptoserviceproviderclasstopic.asp with a small modification t...

String and String[30]
Any advantage in using this style in defining a string type variable? myName: String; hisName: String[30]; "Alan Tse" <alan_NO_SPAM_pltse@yahoo.com.au> wrote in message news:177975@forums.codegear.com... > Any advantage in using this style in defining a string type variable? > > myName: String; > hisName: String[30]; String[30] is a short string type and is ANSI only. You should use simple String type (Unicode in D2009+). -- Wayne Niddery (TeamB) Alan Tse wrote: > Any advantage in using this style in defining a string type variable? ...

Count no of strings in a string
dim ref as string ="<a href=""/iyp/yp/ypkeywordsearch.do?" hi i would like to know something how do i count the number of ref in a html document??you see i have another string call htmldoc(it contains a html document of a web page) and i want to extract all the ref in htmldoc how do i do that? 2) How do i include a closed inverted comma in a string?? dim a as string = "abcde"fghijk""" How do i use the mid method to seacrh from " ?? 1) You should learn about Regular Expressions 2) Double the quotes: "abcde"...

Why String.Split(char[]) is possible but String.Split(string[]) is Not!
Hi everyone, i have a string for e:g String st= "Delete a record****** *************************************************** Delete Manually PatID=123456 PatName=navdeep *****Delete a record****** *************************************************** Delete Manually *****************PatID=123 PatName=navdeep6666" I need to Split this String by "Delete a record" string. Now, str[] will contain the array elements. str = st.Split("Delete a record"); Please Help Regards Navdeep check below link or try code  http://www.codeproject.co...

REPLACE STRING IN STRING
Hello! I use SQLANY 5.5.05 and loking for one function which will do following: find string in string and replace it with another string. In version ASA 6 I found function STUFF which I think do something like this. Is there anything in 5.5.05? Regards Tomaz Try this: create function ReplaceString(in str long varchar,in old_substr char(255),in new_substr char(255)) returns long varchar begin // Change all occurrences of old_substr to new_substr in str. declare start_pos integer; declare found_pos integer; declare new_str long varchar; set new_str=&#...

Difference between String and string
When would you use String as apposed string, when writing functions certain functions work when defined as private string help ( string param){ } Others require the following private string help ( String param){ } Does anybody know why? In C#, string is an alias for the .NET Framework provided type String (and recall C# is case sensitive). I am not aware of any compelling reason to use one vs. the other.Starting with ASP.NET 2.0? Look at:Programming Microsoft Web Forms My Blog...

Detecting string in string
Hello I am not an advanced perl programmer and I do not have my books with me. I want to determine if a string is within a string and to not process the string if the string is found. Could someone please provide a simple sample. Thanks, Andrew On 2/22/07, andrewmchorney@cox.net <andrewmchorney@cox.net> wrote: > I am not an advanced perl programmer Have courage -- you'll get there someday! > and I do not have my books with me. That's what the perldoc command is for. > I want to determine if a string is within a string Do you want the index func...

Web resources about - String and String[] - embarcadero.delphi.win32

Big news from the AP: Hillary Clinton is “preaching love and kindness”
They’re not saying she’s like Jesus. But let’s face it: She’s like Jesus. Actually, to be fair, even the AP finds this hard to take. [W]hen ...

Google Play Movies and TV gets AirPlay support, but Apple TV app still missing
Thanks to recent AirPlay integration, Apple TV owners can now watch movies and TV shows they’ve rented or purchased from the Google Play Movies ...

Google is giving away Star Wars-themed Cardboard virtual reality viewers
Google is continuing its marketing partnership with Lucasfilm, and this time it is giving people a free way to view basic virtual reality content. ...

Paris talks may set a lofty (but meaningless) climate change goal
This story was originally published by The New Republic and is reproduced here as part of the Climate Desk collaboration. Compared to the conferences ...

Five to Try: Yahoo Video Guide sorts streaming options, plus A Good Snowman is Hard to Build
Five to Try: Yahoo Video Guide sorts streaming options, plus A Good Snowman is Hard to Build

Why Banksy just painted Steve Jobs
Syrian refugees are being downtrodden by Republican leaders and presidential candidates aplenty. Rogue graffiti artist Banksy has something to ...

Full Episode: 12/11/15: FBI Searching Lake for Clues in San Bernardino Shooting
Conductor of Boston Runaway Train Placed on Leave; How Moby Dick Inspired Ron Howard's New Film 'In the Heart of the Sea'

Watch: ABC’s ‘Madoff’ Trailer Sees Richard Dreyfuss Channeling the Disgraced Financier
ABC has released the first trailer for its Bernie Madoff miniseries — aptly titled “Madoff” — which will debut on Feb. 3, starring Richard Dreyfuss ...

New York Judge Upholds Injunction Shutting Down Draft Kings And Fan Duel In New York
One month after New York’s Attorney General issued an order banning fantasy sports websites that involve money awards from operating in New York ...

Kristin Cavallari's Brother Michael Did Not Commit Suicide
More details on the death of Kristin Cavallari ‘s brother Michael Cavallari have been revealed and law enforcement officials do not believe he ...

Resources last updated: 12/11/2015 8:18:10 PM