Forum > FPC development

String Type Helper Join

(1/5) > >>

WayneSherman:
I was trying to join strings using helpers and discovered that the value of the string is completely ignored:


--- 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";}};} ---AString := 'C:';//none of these work correctlyAString.Join('\', ['Windows', 'System32']);  //this doesn't do anythingAString := AString.Join('\', ['Windows', 'System32']); //this misses the 'C:', i.e. the contents of AString is ignoredAString := AString + AString.Join('\', ['Windows','System32']); //this doesn't use the separator on the first concatentation. //This works, but uglyAString := 'C:';AString := AString + '\' + AString.Join('\', ['Windows', 'System32']); //or maybe this one is the best wayAString := AString.Join('\', [AString, 'Windows', 'System32']);
Also, it would be nice to have a "PathJoin" helper which automatically uses the correct OS path separator and normalizes the path to remove double separators and fix wrong separators:


--- 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";}};} ---AString := 'C:\Windows\';AString := AString.PathJoin(['\system32\', '/drivers/', 'etc', 'hosts']);// on Windows gives 'C:\Windows\system32\drivers\etc\hosts'// on Linux gives 'C:/Windows/system32/drivers/etc/hosts'

howardpc:

--- Quote from: WayneSherman on May 01, 2022, 05:36:00 pm ---//none of these work correctly
AString.Join('\', ['Windows', 'System32']);  //this doesn't do anything

--- End quote ---
Your assertion is not correct.
It is just that your statement throws away the result of the function.

marcov:
Delphi has a TPath type, I think, for path strings.

Laprado:
To use join function:

AString := string.Join('\', ['c:', 'Windows', 'System32']);

For path you may use ConcatPaths from SysUtils unit:
APath := ConcatPaths(['c:', 'Windows', 'System32']);

trev:

--- Quote from: WayneSherman on May 01, 2022, 05:36:00 pm ---Also, it would be nice to have a "PathJoin" helper which automatically uses the correct OS path separator and normalizes the path to remove double separators and fix wrong separators
--- End quote ---

You are using literal path separators! Instead, use the PathDelim constant.

Navigation

[0] Message Index

[#] Next page

Go to full version