Anonymous user
Substring/Top and tail: Difference between revisions
Add source for Rust
(Add source for Rust) |
|||
Line 616:
end function
puts(1, strip_first("knight"))
puts(1, strip_last("write"))
puts(1, strip_both("brooms"))
=={{header|F_Sharp|F#}}==
Line 1,088:
<lang MATLAB>
% String with first character removed
% String with last character removed
% String with both the first and last characters removed
</lang>
Line 1,351:
<lang Prolog>remove_first_last_chars :-
remove_last(L, LR) :-
Output :
<pre> ?- remove_first_last_chars.
Line 1,476:
(formerly Perl 6)
Raku provides both functional and method forms of substr. Note that, unlike in Perl
We use musical sharps and flats to illustrate that Raku is comfortable with characters from any Unicode plane.
Line 1,618:
print left$(s$,len(s$) -1) 'strip last
print mid$(s$,2,len(s$) -2) 'strip first and last</lang>
=={{header|Rust}}==
One possibility is to modify the owned string representation:
<lang rust>fn main() {
let s = String::from("žluťoučký kůň");
let mut modified = s.clone();
modified.remove(0);
println!("{}", modified);
let mut modified = s.clone();
modified.pop();
println!("{}", modified);
let mut modified = s;
modified.remove(0);
modified.pop();
println!("{}", modified);
}</lang>
Another possibility is to cut a string slice (moreover, this version assumes
nothing about the string length):
<lang rust>fn main() {
let s = "žluťoučký kůň";
println!(
"{}",
s.char_indices()
.nth(1)
.map(|(i, _)| &s[i..])
.unwrap_or_default()
);
println!(
"{}",
s.char_indices()
.nth_back(0)
.map(|(i, _)| &s[..i])
.unwrap_or_default()
);
println!(
"{}",
s.char_indices()
.nth(1)
.and_then(|(i, _)| s.char_indices().nth_back(0).map(|(j, _)| i..j))
.map(|range| &s[range])
.unwrap_or_default()
);
}</lang>
=={{header|Scala}}==
Line 1,693 ⟶ 1,743:
<lang smalltalk>
s := 'upraisers'.
Transcript show: 'Top: ', s allButLast; nl.
Transcript show: 'Tail: ', s allButFirst; nl.
Transcript show: 'Without both: ', s allButFirst allButLast; nl.
Transcript show: 'Without both using substring method: ', (s copyFrom: 2 to: s size - 1); nl.
</lang>
Line 1,803 ⟶ 1,853:
=={{header|Tcl}}==
<lang tcl>puts [string range "knight" 1 end];
puts [string range "write" 0 end-1];
puts [string range "brooms" 1 end-1];
=={{header|TorqueScript}}==
Line 1,878 ⟶ 1,928:
=={{header|VBScript}}==
<lang VBScript>Function TopNTail(s,mode)
End Function
|