Page 1 of 1

Truncate String

Posted: 30 Sep 2016, 13:30
by Capn Odin
I doubt anyone will be interested in this but I made a set of functions to truncate strings from start, end or middle of the string, both in general or only on words.

Code: Select all

Truncate_Left(Length, String, OnWord := False, Dots := True){
	if(StrLength := StrLen(String) > Length){
		result := SubStr(String, StrLength - Length + (Dots ? 3 : 0), Length), WhiteSpace := InStr(" `n", SubStr(String, StrLength - Length + (Dots ? 3 : 0) - 1, 1))
		return (Dots ? "..." : "") . Trim_Trailing_Spaces((OnWord && !WhiteSpace) ? Remove_Until_Word(result, True) : result)
	}
	return String
}

Truncate_Right(Length, String, OnWord := False, Dots := True){
	if(StrLen(String) > Length){
		result := SubStr(String, 1, Length - (Dots ? 3 : 0)), WhiteSpace := InStr(" `n", SubStr(String, Length - (Dots ? 2 : -1), 1))
		return Trim_Trailing_Spaces((OnWord && !WhiteSpace) ? Remove_Until_Word(result) : result) . (Dots ? "..." : "")
	}
	return String
}

Truncate_Middle(Length, String, OnWord := False, Dots := True){
	if(StrLength := StrLen(String) > Length){
		SideLength := Length - (Dots ? 3 : 1), LLen := (SideLength / 2) + (Mod(SideLength, 2) ? 1 : 0), RLen := SideLength / 2
		left  := Truncate_Right(LLen, String, OnWord, False)
		right := Truncate_Left(RLen, String, OnWord, False)
		if(OnWord){
			left2  := Truncate_Right(LLen + (RLen - StrLen(right)), String, OnWord, False)
			right2 := Truncate_Left(RLen + (LLen - StrLen(left)), String, OnWord, False)
		}
		return (OnWord && StrLen(left2) > StrLen(right2) ? left2 : left) . (Dots ? "..." : " ") . (OnWord && StrLen(right2) > StrLen(left2) ? right2 : right)
	}
	return String
}

Remove_Until_Word(String, FromLeft := False){
	if(String) {
		Space := InStr(String, " ", , FromLeft), NewLine := InStr(String, "`n", , FromLeft)
		pos := FromLeft ? (Space > NewLine ? Space : NewLine) : (Space < NewLine ? NewLine : Space)
		Return pos ? (FromLeft ? SubStr(String, pos > 0 ? pos : 1) : SubStr(String, 1, pos > 0 ? pos : 1)) : ""
	}
}

Trim_Trailing_Spaces(String){
	Return RegExReplace(RegExReplace(String, "D)^\s+"), "D)\s+$")
}
The functions returns a substring of the parameter String that they are called with of a length no greater than Length and containing an optional ... at the start, middle or end.
Test

Re: Truncate String

Posted: 03 Oct 2016, 01:39
by ozzii
Cool.
If I may some suggestions for having this a little more complete (at least for me):
-Add to not have the dots (...)
-Add to extract the Nth word (for example word #2 from left or right)
What do you think?

Re: Truncate String

Posted: 03 Oct 2016, 10:37
by Capn Odin
ozzii wrote:-Add to not have the dots (...)
The fourth parameter Dots already allowed for this, however I think I will add a space when calculating Truncate_Middle without dots.
ozzii wrote:-Add to extract the Nth word (for example word #2 from left or right)
I will have to think about it, can't promise anything. It may need its own function ?

Edit: Oh, guess I forgot to explain that the dots where optional, when I described the returned string.

Re: Truncate String

Posted: 04 Oct 2016, 01:38
by ozzii
Didn't saw the dot parameter ;)

Re: Truncate String

Posted: 16 Jul 2019, 09:48
by WilburBr
@Capn Odin
thank you. Very useful :clap: :clap: :clap: