ehigginbotham Posted September 8, 2016 Share Posted September 8, 2016 I am trying to split up a field into the first initials of each word if the string of text is greater than 31 characters. example if the field contains less than 31 characters, return the filed. If it contains more than 31 characters return the first letter of each word in the field. I tried to do a basic split on the " " and I gave it up to 6 possible variable splits. Then I tried to trim the first letter off of each variable split. It works when there are 6 words to split, but it returns an error when there are fewer. There is probably a much easier way to do this, but here is the code I have so far. (I haven't even gotten to checking the field for the number of characters yet, so it there is an easy way to do that, can you throw that in also. CustTypeSplit=Field("NAME1").split(" ") first=CustTypeSplit[0] middle=CustTypeSplit[1] last=CustTypeSplit[2] last2=CustTypeSplit[3] last3=CustTypeSplit[4] last4=CustTypeSplit[5] if (first == "") var1="" else var1=Trim(Left(first, "1")) if (middle == "") var2="" else var2=Trim(Left(middle, "1")) if (last == "") var3="" else var3=Trim(Left(last, "1")); if (last2 == "") var4="" else var4=Trim(Left(last2, "1")); if (last3 == "") var5="" else var5=Trim(Left(last3, "1")); if (last4 == "") var6="" else var6=Trim(Left(last4, "1")); if (Len(Field("NAME1")>=31)) return var1 +" "+ var2 + " " + var3 + " " + var4 + " " + var5 +" "+var6; else return Field("NAME1"); thanks in advance Eric Quote Link to comment Share on other sites More sharing options...
step Posted September 8, 2016 Share Posted September 8, 2016 I am trying to split up a field into the first initials of each word if the string of text is greater than 31 characters. You can determine the length of a string by accessing the '.length' property. Keep in mind, though, that spaces are included in that value: return 'A String'.length; // returns 8 I tried to do a basic split on the " " and I gave it up to 6 possible variable splits. Then I tried to trim the first letter off of each variable split. It works when there are 6 words to split, but it returns an error when there are fewer. Splitting a string creates an array of strings which are accessible by calling their position in the array. So that works fine when there are 6 words but asking for the 6th word (CustTypeSplit[5]) in a two-word array will return an error because that position doesn't exist: return ["One","Two"][5]; // undefined You can account for that by writing it like this: CustTypeSplit = Field("NAME1").split(" "); first = CustTypeSplit[0] || ""; middle = CustTypeSplit[1] || ""; last = CustTypeSplit[2] || ""; last2 = CustTypeSplit[3] || ""; last3 = CustTypeSplit[4] || ""; last4 = CustTypeSplit[5] || ""; There is probably a much easier way to do this, but here is the code I have so far. Yeah, there is. You can do it like this: var field = Field("NAME1"); if (field.length > 31) { field = field.split(' ').map(function(s) { return Left(s,1); }).join(' '); } return field; Or: return (f = Field("NAME1")).length > 31 ? f.split(' ').map(function(s) { return Left(s, 1); }).join(' ') : f; Quote Link to comment Share on other sites More sharing options...
ehigginbotham Posted September 9, 2016 Author Share Posted September 9, 2016 that worked great!!! thanks so much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.