Discussion:
escaping newline in multi-line strings
Lorenzo Bettini
13 years ago
Permalink
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
13 years ago
Permalink
Hi Lorenzo,

'''
foo.«
»bar
'''

would do the trick.

Sven
...
Lorenzo Bettini
13 years ago
Permalink
Uh! Cool! :)
is it documented somewhere?

thanks Sven!
cheers
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
h***@gmail.com
10 years ago
Permalink
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»
'''
...
--
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.
Sven Efftinge
10 years ago
Permalink
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
10 years ago
Permalink
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: ***@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
...
--
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.
Sven Efftinge
10 years ago
Permalink
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.
...
--
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
10 years ago
Permalink
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.
...
--
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.
minna hu
10 years ago
Permalink
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()»"



On Tue, Apr 21, 2015 at 12:03 AM, Sebastian Zarnekow <
...
--
Best Regards,
Minna Hu
--
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.
minna hu
10 years ago
Permalink
Oh, it complained about ".into(new ArrayList)[0]". Thanks.
...
--
Best Regards,
Minna Hu
--
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.
Stefan Oehme
10 years ago
Permalink
There is no "[0]" syntax in Xtend. Please use ".head" or ".get(0)"
...
--
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.
minna hu
10 years ago
Permalink
.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()»"
...
--
Best Regards,
Minna Hu
--
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.
Sven Efftinge
10 years ago
Permalink
Just in case you are interested in yet another improvement :-)

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


 will do it as well.
...
--
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.
minna hu
10 years ago
Permalink
Yes, findFirst also worked for me, and it makes the code clearer, thank
you, Sven.
...
--
Best Regards,
Minna Hu
--
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.
Continue reading on narkive:
Loading...