A general solution with expl3
; the command \foo
has an optional argument that can be any character or box to mark the splitting point. With varwidth
we round the box width to what is really necessary.
\documentclass{article}
\usepackage{varwidth}
\usepackage{xparse}
\setlength{\parindent}{0pt} % just for the example
\ExplSyntaxOn
\NewDocumentCommand{\foo}{ O{} m m }
{
% some setup for the particular problem
\begin{varwidth}[t]{#2}
% some padding on the right
\skip_set:Nn \rightskip { 0pt plus .5em }
% just in case
\skip_set:Nn \leftskip { 0pt }
% a strut to set the first row height
\strut
% the main routine
\mayer_foo:nnn { #1 } { #2 } { #3 }
% finish off with a strut (attached from the last digit)
\strut
\end{varwidth}
}
\seq_new:N \l_mayer_input_seq
\cs_new_protected:Npn \mayer_foo:nnn #1 #2 #3
{
% split the input into pieces
\seq_set_split:Nnn \l_mayer_input_seq { } { #3 }
% between any two pieces insert a discretionary
\seq_use:Nn \l_mayer_input_seq { \discretionary{#1}{}{} }
}
\ExplSyntaxOff
\begin{document}
\rule{2in}{0.4pt}
\foo{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}
\foo[-]{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}
\foo[*]{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}
\bigskip
The box doesn't take more than the stated space\\
(but it can be slightly less wide)
\medskip
\fbox{\foo{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}}
\end{document}

For completeness, a version where the normal text width is used, without boxing.
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\foo}{ O{} m }
{% #1 = discretionary, #2 = number to split
\mayer_foo:nn { #1 } { #2 }
}
\cs_new_protected:Nn \mayer_foo:nn
{
\tl_map_inline:nn { #2 }
{
##1 \nobreak\hspace{0pt plus 0.1pt}\discretionary{#1}{}{}
}
}
\ExplSyntaxOff
\begin{document}
\noindent
\foo{
17823 94123 74908 12347 12390 47123 89407 12340 89172 34890
12374 89120 34712 98571 23890 12351 12374 89120 34712 98571
17823 94123 74908 12347 12390 47123 89407 12340 89172 34890
12374 89120 34712 98571 23890 12351 12374 89120 34712 98571
17823 94123 74908 12347 12390 47123 89407 12340 89172 34890
12374 89120 34712 98571 23890 12351 12374 89120 34712 98571
}
\noindent
\foo[-]{
17823 94123 74908 12347 12390 47123 89407 12340 89172 34890
12374 89120 34712 98571 23890 12351 12374 89120 34712 98571
17823 94123 74908 12347 12390 47123 89407 12340 89172 34890
12374 89120 34712 98571 23890 12351 12374 89120 34712 98571
17823 94123 74908 12347 12390 47123 89407 12340 89172 34890
12374 89120 34712 98571 23890 12351 12374 89120 34712 98571
}
\noindent
\foo[\hbox{$*$}]{
17823 94123 74908 12347 12390 47123 89407 12340 89172 34890
12374 89120 34712 98571 23890 12351 12374 89120 34712 98571
17823 94123 74908 12347 12390 47123 89407 12340 89172 34890
12374 89120 34712 98571 23890 12351 12374 89120 34712 98571
17823 94123 74908 12347 12390 47123 89407 12340 89172 34890
12374 89120 34712 98571 23890 12351 12374 89120 34712 98571
}
\end{document}

The example shows that if the discretionary to be used is something more complex than a character, it should be enclosed in \hbox
.