Kentico - Transformation Eval() & trailing space at the end - kentico

This must be the simplest question ever, but again, I don't know the answer. I've just noticed that (in my case) using something like Eval("Location") always creates a trailing blank space at the end of the output. Normally I don't care about that trailing space, but in one particular situation, it has to be removed. I've tried using replace() but that only works for the text itself but not the trailing blank space, such as "San Francisco " becomes "SanFrancisco ", but the trailing space still exists. Please let me know how to get rid of it. I've checked my text, and it doesn't have any space at the end.
Eval("Location").ToString().Replace(" ","")

The function you are looking for is .Trim() which will remove both trailing and leading blank spaces from a string. So you can use
Eval("Location").ToString().Trim()
However, if using .Replace() isn't removing that trailing space, then I would say the space is not coming from the field itself, but rather from something after it in the transformation.
If your code is like:
"<%#Eval("Location").ToString().Trim()%> [other content]" then there will always be a space between that field content and the rest of the content. Perhaps check the transformation and see if there is a space after you are evaluating that field?

As Brandon already mentioned, the function you're looking for is .Trim() This only works on strings though. So if it's not working, you will most likely need to cast that object to a string using one of the following:
ValidationHelper.GetString(Eval("Location"), "").Trim()
Eval<string>("Location").Trim()

Related

Excel/CSV - putting a single quote to keep leading zeros does not work on a Mac

Other suggested to keep the leading zeros in Excel/CSV, I can just add the single quote character. However, this does not work on a Mac properly. The single quote shows up, which is something that I do not want.
Please see image for more details.
https://social.msdn.microsoft.com/Forums/en-US/aae07b39-865f-4c68-a07f-7cad2dfd6733/how-do-i-open-csv-using-excel-without-deleting-leading-zeros?forum=isvvba
I can just use "=""00100""" to keep the leading zeros.

Why this excel string comparison return fail?

Is it an Excel bug? Anyone have experienced this issue, please help?
Just a thought but here's what MS says about TRIM
The TRIM function was designed to trim the 7-bit ASCII space character
(value 32) from text. In the Unicode character set, there is an
additional space character called the nonbreaking space character that
has a decimal value of 160. This character is commonly used in Web
pages as the HTML entity, . By itself, the TRIM function does
not remove this nonbreaking space character.
you might try this to replace the non-breaking space (if that is your problem here).
=TRIM(SUBSTITUTE(A5,CHAR(160),CHAR(32)))
I would have to agree with #Jeeped. Your formula looks correct in all aspects. It must be a non-printing character. If this data is coming from some outside source (I.e. another file) then there very well could be a non-printed character. I just typed in everything you had manually and came up with this.

Moving Columns/Text in VIM

I was wondering how I might go about moving around Columns/Text around in VIM using a string. I have a short list of names I have to reorder, which need to be placed in Last Name First Middle to First Middle Last.
So here would be an example list:
Plant, Robert A.
Page, Jimmy
Bonhham, John H.
Jones, John Paul
I was thinking that the string should look something like this:
:s/\([A-z]\{2}\)\(\[A-z]\{2}\)/2\1/
Thanks
First, I recommend using the \v "very magic" flag to avoid all the other internal escaping of metacharacters. This will work with a replacement like:
:s/\v([A-z]+),\s+([A-z]+)(\s+[A-z.]+)?/\2\3 \1
Breaking it down:
([A-z]+) Capture the last name into \1
,\s+ A literal comma and one or more spaces
([A-z]+) Capture the first name into \2
(\s+[A-z.]+)? Capture the middle name with its leading spaces, since it may not exist. Also permit the ., and end with a ? to make the whole construct optional, into \3
\2\3 \1 Replace with the second group (first name) followed immediately by the middle name \3 with no space in between because the space was captured along with the middle name. Then append \1 the last name.
If the names could be possibly more than [A-z]+, you may alternatively use [\S]+ to capture all non-whitespace characters.
How about this:
:%s/\([[:alpha:]]\+\), \([[:alpha:]]\+\)\( [[:alpha:]]\+\.\?\)\?/\2\3 \1/g
This captures last, middle (optional), and first, and reorders them in the replacement. You'll probably need to include additional characters in the [[:alpha:]] collection, but this works for your example.
For more information, learn how the excellent and comprehensive :help is structured; all the information is in there (you just need to know how to find it)! There are also many very similar regular expression questions here on Stack Overflow.

Replace character with a safe character and vice-versa

Here's my problem:
I need to store sentences "somewhere" (it doesn't matter where).
The sentences must not contain spaces.
When I extract the sentences from that "somewhere", I need to restore the spaces.
So, before storing the sentence "I am happy" I could replace the spaces with a safe character, such as &. In C#:
theString.Replace(' ', '&');
This would yield 'I&am&happy'.
And when retrieving the sentence, I would to the reverse:
theString.Replace('&', ' ');
But what if the original sentence already contains the '&' character?
Say I would do the same thing with the sentence 'I am happy & healthy'. With the design above, the string would come back as 'I am happy healthy', since the '&' char has been replaced with a space.
(Of course, I could change the & character to a more unlikely symbol, such as ยค, but I want this to be bullet proof)
I used to know how to solve this, but I forgot how.
Any ideas?
Thanks!
Fredrik
Maybe you can use url encoding (percent encoding) as an inspiration.
Characters that are not valid in a url are escaped by writing %XX where XX is a numeric code that represents the character. The % sign itself can also be escaped in the same way, so that way you never run into problems when translating it back to the original string.
There are probably other similar encodings, and for your own application you can use an & just as well as a %, but by using an existing encoding like this, you can probably also find existing functions to do the encoding and decoding for you.

When is it acceptable to not trim a user input string?

Can someone give me a real-world scenario of a method/function with a string argument which came from user input (e.g. form field, parsed data from file, etc.) where leading or trailing spaces SHOULD NOT have been trimmed?
I can't ever recall such a situation for myself.
EDIT: Mind you, I didn't say trimming any whitespace. I said trimming leading or trailing (only) spaces (or whitespace).
Search string in any "Find" dialog in an editor.
Password input boxes. There's lots of data out there, where whitespace can genuinely be considered important part of the string. It narrows things down alot by making it starting and ending whitespace only, but there's still many examples. Stuff you pass through a PHP style nl2br function.
If you are inputting code. There may be a scenario where whitespace at the begining and end are necessary.
Also, look at Stack Overflow's markdown editor. Code examples are indented. If you posted just a code example, then it will require leading and trailing white space not be trimmed.
Perhaps a Whitespace interpreter.
Python....
A Stackoverflow answer, or more generally input written in markdown (four leading spaces -> code block).
A paragraph entry.
If the input is python code (say, for a pastebin kinda thing), you certainly can't trim leading white space; but you also can't trim trailing white space, because it could be a part of a multi-line string (triple quoted string).
I've used whitespace as a delimiter before, so there. Also, for anything that involves concatenating multiple inputs, removing leading/trailing whitespace can break formatting or possibly do worse. Aside from that, as Spencer said, for indented paragraphs you probably would not want to remove the leading whitespace.
Obviously passwords should not be trimmed. Passwords can contain leading or trailing whitespaces that need to be be treated as valid characters.

Resources