Truncate String

Post your working scripts, libraries and tools
User avatar
Capn Odin
Posts: 1290
Joined: 23 Feb 2016, 19:45
Location: Denmark

Truncate String

30 Sep 2016, 13:30

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
Last edited by Capn Odin on 03 Oct 2016, 11:23, edited 3 times in total.
Please excuse my spelling I am dyslexic.
ozzii
Posts: 310
Joined: 30 Oct 2013, 06:04

Re: Truncate String

03 Oct 2016, 01:39

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?
User avatar
Capn Odin
Posts: 1290
Joined: 23 Feb 2016, 19:45
Location: Denmark

Re: Truncate String

03 Oct 2016, 10:37

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.
Please excuse my spelling I am dyslexic.
ozzii
Posts: 310
Joined: 30 Oct 2013, 06:04

Re: Truncate String

04 Oct 2016, 01:38

Didn't saw the dot parameter ;)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Banayat and 29 guests