Java Reference
In-Depth Information
This operation goes one step further than the
trim()
operation by remov-
ing excess whitespace within a String and also removing leading and trailing
whitespace. Whitespace sequences are replaced by a single space in the returned
String.
log
('A sentence with extra spaces '.normalizeSpace());
result: A sentence with extra spaces
replace
String::replace (m1:String, m2:String): String
Returns a String with all occurrences of String
m1
replaced with String
m2
.
Following is an example of package
.
(dot) notation replaced with directory path
delimiters (/). (Note the escape character before the dot:
var
pkg :
String
:= 'org.eclipse.mindmap';
log
('Converted package to path', pkg.replace('\.', '/'));
The specification indicates that
replace()
will work on all occurrences, but
we see here that only the first is replaced:
Converted package to path, data: org/eclipse.mindmap
Eclipse M2M QVT OML provides a
replaceAll()
operation that does
what we expect:
var
pkg :
String
:= 'org.eclipse.mindmap';
log
('Converted package to path', pkg.replaceAll('\.', '/'));
Now we get what we wanted in the console output:
Converted package to path, data: org/eclipse/mindmap
If we had not used the escape character in the first example, the output
would have been this:
Converted package to path, data: /rg.eclipse.mindmap
Search WWH ::
Custom Search