Recent

Author Topic: Make StringList from Unicode string with '\n'  (Read 1989 times)

Sniper

  • New Member
  • *
  • Posts: 39
Make StringList from Unicode string with '\n'
« on: April 25, 2018, 03:44:58 pm »
I have unicode string from THtmlviewer.DocumentSource.

Now I need to go through this string.

some pseudocode:
Code: Pascal  [Select][+][-]
  1. var
  2. HTMLString: WideString;
  3. i:integer
  4. fList:TStringList;
  5. fList := TStringList.Create();
  6.     while (true) do
  7.     begin
  8.  
  9.       fList.Add();
  10.       if (HTMLString[i] <> '\n')
  11.          break;
  12.       inc(i);
  13.     end;    
  14.  

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Make StringList from Unicode string with '\n'
« Reply #1 on: April 25, 2018, 04:16:28 pm »
Code: [Select]
if (HTMLString[i] <> '\n')

Htmlstring\[i\] is one char.  '\n' is two, so they are never equal ?

Maybe you try something else than a backslash and an n?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Make StringList from Unicode string with '\n'
« Reply #2 on: April 25, 2018, 04:28:14 pm »
stringlist already breaks on line ends just do
Code: Pascal  [Select][+][-]
  1. var
  2. HTMLString: WideString;
  3. i:integer
  4. fList:TStringList;
  5. begin
  6.   fList := TStringList.Create();
  7.   fList.Text := HTMLString;
  8. end;
  9.  
that should give you multiple lines. as far as I know there is no escape character support in string in pascal you have to define your character by code I suggest to use #10 it will always break correctly the lines regardless of line end format.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Sniper

  • New Member
  • *
  • Posts: 39
Re: Make StringList from Unicode string with '\n'
« Reply #3 on: April 25, 2018, 04:32:29 pm »
Code: [Select]
if (HTMLString[i] <> '\n')

Htmlstring\[i\] is one char.  '\n' is two, so they are never equal ?

Maybe you try something else than a backslash and an n?
End of line always \r\n or #10#13. I don't know what it should be here.

Sniper

  • New Member
  • *
  • Posts: 39
Re: Make StringList from Unicode string with '\n'
« Reply #4 on: April 25, 2018, 04:33:38 pm »
stringlist already breaks on line ends just do
Code: Pascal  [Select][+][-]
  1. var
  2. HTMLString: WideString;
  3. i:integer
  4. fList:TStringList;
  5. begin
  6.   fList := TStringList.Create();
  7.   fList.Text := HTMLString;
  8. end;
  9.  
that should give you multiple lines. as far as I know there is no escape character support in string in pascal you have to define your character by code I suggest to use #10 it will always break correctly the lines regardless of line end format.
Nice. What a simple solution! I'll try it.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Make StringList from Unicode string with '\n'
« Reply #5 on: April 25, 2018, 04:39:48 pm »
Code: [Select]
if (HTMLString[i] <> '\n')

Htmlstring\[i\] is one char.  '\n' is two, so they are never equal ?

Maybe you try something else than a backslash and an n?
End of line always \r\n or #10#13. I don't know what it should be here.
JFYI \r\n = #13#10
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Make StringList from Unicode string with '\n'
« Reply #6 on: April 25, 2018, 06:15:14 pm »
Pascal generally does not interpret  values in literal string like 'xxx'.   Such backslash notation is originally C.

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Make StringList from Unicode string with '\n'
« Reply #7 on: April 25, 2018, 08:54:26 pm »
We have a nice global constant for that called LineEnding
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018