Forum > Beginners
Split a string in words
Thaddy:
Modern version:
--- 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";}};} ---program splitme;uses sysutils;var a:TstringArray; b:AnsiString = 'Costica;Ana;Andrei;Ema'; c:AnsiString;begin a:=b.Split([';']); for c in a do writeln(c);end.
BTW: what's wrong with the code editor? It now escapes the string quotes. That was never the case..
[edit] txs for fixing
bytebites:
--- 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";}};} ---program example;uses sysutils;var s:String; c:char;begin s:= 'Costica;Ana;Andrei;Ema'; for c in s do if c=';' then writeln else write(c); writeln;end.
Thaddy:
Mine can be a bit bit shorter and without branching in the maiin code;
--- 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";}};} ---program splitme2;uses sysutils;var a:TstringArray; b:AnsiString = 'Costica;Ana;Andrei';begin a:=b.Split([';']); for b in a do writeln(b); // re-use bend.
PascalDragon:
--- Quote from: funlw65 on January 05, 2022, 05:33:20 am ---Thank you for the links, I prefer not to use dynamic allocation...
--- End quote ---
Why not? Are there specific reasons for that? Cause you're restricting yourself quite a bit without that.
Thaddy:
@tr_escape
Splitstring is - see your own link - a convenience wrapper around ansistring.split() so why not use string.split() directly? You then do not have to pull in the strutils unit.
Navigation
[0] Message Index
[#] Next page
[*] Previous page