escaping newline in multi-line strings

647 views
Skip to first unread message

Lorenzo Bettini

unread,
Dec 16, 2011, 9:17:17 AM12/16/11
to xtend...@googlegroups.com
Hi

If I have a multi line string

'''
foo.
bar
'''

is there a way of escaping the newline char so that the result is actually

foo.bar

?

I'm asking since in my xtend2 based generator I'd like to split the
multi line string just for formatting purposes, but I don't want the
newline appear in the result

thanks in advance
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com


Sven Efftinge

unread,
Dec 16, 2011, 9:34:13 AM12/16/11
to xtend...@googlegroups.com
Hi Lorenzo,

'''
foo.«
»bar
'''

would do the trick.

Sven

Lorenzo Bettini

unread,
Dec 16, 2011, 11:55:27 AM12/16/11
to xtend...@googlegroups.com
Uh! Cool! :)
is it documented somewhere?

thanks Sven!
cheers
Lorenzo

huan...@gmail.com

unread,
Apr 20, 2015, 3:21:26 PM4/20/15
to xtend...@googlegroups.com, lorenzo...@gmail.com
Hi Sven and Lorenzo,

I have the following code, xtend always generates a new line after c.value.toString(), do you know how can I avoid that new line? Thanks!

'''
«IF !consts.empty»
            «FOR c : consts»
                «IF c.name == "foo"»
                "«c.value.toString()»"
                «ENDIF»
            «ENDFOR»
        «ENDIF»
'''

Sven Efftinge

unread,
Apr 21, 2015, 2:15:19 AM4/21/15
to xtend...@googlegroups.com
c.value seems to have the new line. You can remove any surrounding whitespace with trim().

   c.value.toString.trim

--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sebastian Zarnekow

unread,
Apr 21, 2015, 2:32:57 AM4/21/15
to xtend...@googlegroups.com, lorenzo...@gmail.com
Hi,

your template has a newline after «c.value.toString()» thus it will always emit the newline. If you want to drop that, you'd need to make it a one-liner, e.g. «c.consts.filter[name == 'foo'].join[c.value.toString]». If you see more than one newline char for each c, that it's the value itself, that contains it.

Best,
Sebastian

Sebastian Zarnekow
Xtext Committer

mobile:    +49 (0) 151 / 1739 6724
web:    http://www.itemis.de
mail:    Sebastian...@itemis.de
xing:    http://www.xing.com/profile/Sebastian_Zarnekow
blog:    http://zarnekow.blogspot.com

itemis AG
Am Germaniahafen 1
24143 Kiel
Germany

Rechtlicher Hinweis:
Amtsgericht Dortmund, HRB 20621
Vorstand: Jens Wagener (Vors.), Wolfgang Neuhaus, Dr. Georg Pietrek, Jens Trompeter, Sebastian Neus
Aufsichtsrat: Prof. Dr. Burkhard Igel (Vors.), Michael Neuhaus, Jennifer Fiorentino

Sven Efftinge

unread,
Apr 21, 2015, 2:49:23 AM4/21/15
to xtend...@googlegroups.com
You can tell which solution solves your problem by the position of the newline.
If it is as you said behind c.value.toString the newline is in the value.
If you want to get rid of the newline after the “ then Sebastian’s tip will help.

Sebastian Zarnekow

unread,
Apr 21, 2015, 3:03:39 AM4/21/15
to xtend...@googlegroups.com, lorenzo.bettini
My bad, I didn't recognize the quotes around your «c.value.toString» thus the join won't work as described but you'd have to provide before, after, and separator, too.

minna hu

unread,
Apr 21, 2015, 7:22:53 PM4/21/15
to xtend...@googlegroups.com, lorenzo.bettini
Thank you very much for your tips! I troubleshooted, the value itself does not contain newline. We need to convert the code into one liner to avoid the new line.

I have tried to translate my code into one-liner, however, it generated compilation error. Do you know how to fix it? Thanks! I am not sure how to use "before, after, and separator", please let me know. Thanks!

I need to get the constant with its name equals to "foo", and then get value of this constant.

Old code:

'''
«IF !consts.empty»
            «FOR c : consts»
                «IF c.name == "foo"»
                "«c.value.toString()»"
                «ENDIF»
            «ENDFOR»
        «ENDIF»
'''

to one-liner code:


"«consts.filter[name == "foo"].into(new ArrayList)[0].getValue().toString()»"



--
You received this message because you are subscribed to a topic in the Google Groups "Xtend Programming Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/xtend-lang/WlM7qzaeyis/unsubscribe.
To unsubscribe from this group and all its topics, send an email to xtend-lang+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Best Regards,
Minna Hu

minna hu

unread,
Apr 21, 2015, 7:23:31 PM4/21/15
to xtend...@googlegroups.com, lorenzo.bettini
Oh, it complained about ".into(new ArrayList)[0]". Thanks.

Stefan Oehme

unread,
Apr 22, 2015, 1:58:04 AM4/22/15
to xtend...@googlegroups.com, huan...@gmail.com, lorenzo...@gmail.com
There is no "[0]" syntax in Xtend. Please use ".head" or ".get(0)"

minna hu

unread,
Apr 22, 2015, 12:26:23 PM4/22/15
to Stefan Oehme, xtend...@googlegroups.com, lorenzo.bettini
.into(new ArrayList) didn't work for me. I removed .into(new ArrayList), changed to get(0) and used the iterator returned by the filter method directly, it worked, thank you, guys!

Here is the working version:
"«consts.filter[name == "foo"].get(0).getValue().toString()»"

Sven Efftinge

unread,
Apr 22, 2015, 2:46:15 PM4/22/15
to xtend...@googlegroups.com, Stefan Oehme, lorenzo.bettini
Just in case you are interested in yet another improvement :-)

  «const.findFirst[name==“foo”].value.toString»

… will do it as well.

minna hu

unread,
Apr 22, 2015, 4:28:56 PM4/22/15
to xtend...@googlegroups.com, Stefan Oehme, lorenzo.bettini
Yes, findFirst also worked for me, and it makes the code clearer, thank you, Sven. 
Reply all
Reply to author
Forward
0 new messages