INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Need help with an expression to replace whitespaces with dashes  (Read 2192 times)

Mars

  • World Citizen
  • ***
  • Posts: 199

Hi,

I need an expression to replace whitespaces with dashes in a field.

I've tried:

Code: [Select]
Replace ([Artist],  , -)
Replace ([Artist], ' ', -)
Replace ([Artist], " ", -)

but none seems to work...
Logged

blgentry

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 8014
Re: Need help with an expression to replace whitespaces with dashes
« Reply #1 on: April 08, 2016, 05:44:58 pm »

When you write expressions, MC generally discards white space.  This makes sense most of the time because spacing between words, functions, etc really shouldn't matter when you're writing an expression.

But what about those times that you *want* MC to see your space characters and take them into account?  The answer is to use a quoting character to tell MC, "Hey, everything after this is a literal character, and you shouldn't throw any of them away or assign special meaning to them.  They are literally what is written."

MC's primary quoting character is the slash (/).  So the expression you want is:

Code: [Select]
replace([name],/ ,-)
The space after the slash will be interpreted as a literal space, which is what you want.  Now if you want to turn multiple spaces in a row into dashes, then it becomes another game.  Which MC can do, if you need it to.

Brian.
Logged

Mars

  • World Citizen
  • ***
  • Posts: 199
Re: Need help with an expression to replace whitespaces with dashes
« Reply #2 on: April 08, 2016, 05:54:43 pm »

Thank you Brian! Didn't know about the slash being the quoting mark...
Logged

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Need help with an expression to replace whitespaces with dashes
« Reply #3 on: April 08, 2016, 07:14:29 pm »

The / character is a single-character escape* (meaning, the character that follows a slash is passed through without normal processing). You can also do a multi-character escape with /# string literal goes here #/.

Lots More:
http://wiki.jriver.com/index.php/Expression_Language#Expression_Language_Syntax

* Just FYI, it is called, as is typical in programming, an escape character (as opposed to a "quoting character").
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/
Pages: [1]   Go Up