cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
joelahn
Level III

How do we add a whitespace within the string in one of lists of which specific character length bigger than standard form?

For an example;

 

Character Column#1 at row#1: asd 123 deg jkldjsakldjlk 1124

 

if length of any of word within the cell value is bigger than "5", I want a whitespace on the third character.

 

Ideal form is as below

Character Column#1 at row#1: asd 123 deg jkl djsakldjlk 1124

 

thanks,

Joel Ahn
Lam Research Corporation
Mobile +82 10 9509 0109
3 REPLIES 3
jthi
Super User

Re: How do we add a whitespace within the string in one of lists of which specific character length bigger than standard form?

One way would be to use Regex.

 

This might work:

Names Default To Here(1);

line = "asd 123 deg jkldjsakldjlk 1124";
span_line = Regex(line, "([a-zA-Z0-9]{3})([a-zA-Z0-9]{3,})", "\1 \2", GLOBALREPLACE);
show(span_line == "asd 123 deg jkl djsakldjlk 1124");

line = "1 22 333 4444 55555 666666 7777777 88888888 999999999 0000000000";
span_line = Regex(line, "([a-zA-Z0-9]{3})([a-zA-Z0-9]{3,})", "\1 \2", GLOBALREPLACE);
show(span_line);

https://regex101.com/r/CuxFKx/1 

 

-Jarmo
txnelson
Super User

Re: How do we add a whitespace within the string in one of lists of which specific character length bigger than standard form?

Here is one way of doing what you want

Names Default To Here( 1 );
dt = Current Data Table();

For Each Row(
	holdString = "";
	wordNum = 1;
	While( Word( wordNum, :column 1, " " ) != "",
		theWord = Word( wordNum, :column 1, " " );
		If( Length( theWord ) > 5,
			theWord = Substr( theWord, 1, 3 ) || " " || Substr( theWord, 4 )
		);
		holdString = holdString || " " || theWord;
		wordNum++;
	);
	:Column 1 = Trim( holdString );
Jim

Re: How do we add a whitespace within the string in one of lists of which specific character length bigger than standard form?

I am not sure if you an interactive, a formula, or a scripted solution. The Cols > Recode command provides a rich tool for changing values in a column. See examples and instructions for Recode here.