

InterviewSolution
Saved Bookmarks
1. |
Solve : Separate String with No Delimiters? |
Answer» <html><body><p>Is it possible to tokenize a given string, character by character, when no <a href="https://interviewquestions.tuteehub.com/tag/delimiters-947634" style="font-weight:bold;" target="_blank" title="Click to know more about DELIMITERS">DELIMITERS</a> are present? E.G., if the string contained "abcd" instead of "a,b,c,d".<br/><br/>Or, from the opposite way of thinking, is it possible to automatically insert delimiters into a string once per character?<br/><br/>Perhaps you would have to build the string, character by character using string manipulation on your input....but how could you automate string manipulation commands(:~x,y) to move/progress that way?Hi Greg.<br/><br/>Here's a start for you to improve on. The addition of the # char to the string is just to <a href="https://interviewquestions.tuteehub.com/tag/give-468520" style="font-weight:bold;" target="_blank" title="Click to know more about GIVE">GIVE</a> something to test for end-of-string and can be any character. The For loop is just to demo output.<br/><br/> Code: <a>[Select]</a>echo off<br/>cls<br/><br/>setlocal enabledelayedexpansion <br/><br/>set string=abcdefg#<br/>set pos=0<br/><br/>:start<br/> <a href="https://interviewquestions.tuteehub.com/tag/call-412416" style="font-weight:bold;" target="_blank" title="Click to know more about CALL">CALL</a> set chr=%%string:~%pos%,<a href="https://interviewquestions.tuteehub.com/tag/1-236780" style="font-weight:bold;" target="_blank" title="Click to know more about 1">1</a>%%<br/> if !chr!==# goto finis<br/> set var1=!var1!!chr!,<br/> set /a pos+=1<br/> goto start<br/><br/>:finis<br/>set var1=!var1:~0,-1!<br/><br/><br/>for /<a href="https://interviewquestions.tuteehub.com/tag/f-236701" style="font-weight:bold;" target="_blank" title="Click to know more about F">F</a> "tokens=1-%pos% delims=," %%A in ("%var1%") do (<br/> set var2=%%A%%B<br/> set var3=%%B%%F%%G<br/>)<br/>echo String Variable =%string:~0,-1%<br/>echo CSV string =!var1!<br/>echo Tokens=Pos=!pos!<br/>echo Var2=%var2%<br/>echo Var3=%var3%<br/><br/></p></body></html> | |