Forum > Beginners

Need actual Char Count of a String

(1/5) > >>

nugax:
without using TStrings and just using a Normal string var, how can I get the actual char count in the variable, not the size of the field. For example:

if i have
String[36] := 'test';

length(string) would return 36. So would sizeof(var).

I need to just know (in order to print) the actual characters and not 30 null spaces.
There has to be a routine without me having to write one in FP, that counts actual chars in a string?

Here is what im doing, if it matters:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  //Write Begining Msg Header Info  blockwrite(F, MH.OrigNode, sizeof(MH.OrigNode));  blockwrite(F, MH.OrigNet, sizeof(MH.OrigNet));  blockwrite(F, MH.DestNode, sizeof(MH.DestNet));  blockwrite(F, MH.Cost, sizeof(MH.Cost));  blockwrite(F, MH.Attribute, sizeof(MH.Attribute));   

Handoko:

--- Quote from: nugax on April 07, 2022, 03:05:06 pm ---if i have
String[36] := 'test';

length(string) would return 36. So would sizeof(var).

--- End quote ---

I got 4 and 37 if I run this code in Lazarus:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  S: string[36];begin  S := 'test';  ShowMessage(Length(S).ToString);  ShowMessage(SizeOf(S).ToString);end;

nugax:
Interesting…

When is use blockwrite - it writes the chars then the rest a null, chr(0)

Handoko:
Using BlockWrite requires you fully understand how to arrange the data in memory. When I was learning and creating my own-invent database, I was having similar problem saving string data to file using BlockWrite. After some careful inspections and after better understand how to 'map' the data to memory, I managed to make my database to work correctly. Unfortunately, it's too slow so I abandoned it.

Please provide a compile-able source code so we can try, inspect and tell where you did it wrong.


--- Quote from: nugax on April 07, 2022, 03:25:07 pm ---When is use blockwrite - it writes the chars then the rest a null, chr(0)

--- End quote ---

Handling string is an 'expensive' thing. Pascal makes it easy but without you knowing, it actually doing a lot of things. What you've found may seem weird to you but if you understand deeper, you will appreciate how awesome Pascal managing string.

Warfley:
May I ask why you are using shortstrings in the first place if you are going to write them dynamically anyway?

You could simply use a filestream and dynamic strings:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// Writevar  fs: TFileStream;  s: String;begin  fs := TFileStream.Create(filename, fmCreate);  try    fs.WriteAnsiString(s);  finally    fs.Free;  end;end; // readvar  fs: TFileStream;  s: String;begin  fs := TFileStream.Create(filename, fmOpenRead);  try    s := fs.ReadAnsiString;  finally    fs.Free;  end;end;
You don't have to reinvent the wheel here. Writing dynamic length strings is already provided

Navigation

[0] Message Index

[#] Next page

Go to full version