Substring/Top and tail: Difference between revisions

Add source for Rust
(Add source for Rust)
Line 616:
end function
 
puts(1, strip_first("knight")) -- strip first character
puts(1, strip_last("write")) -- strip last character
puts(1, strip_both("brooms")) -- strip both first and last characters</lang>
 
=={{header|F_Sharp|F#}}==
Line 1,088:
<lang MATLAB>
% String with first character removed
str(2:end)
% String with last character removed
str(1:end-1)
% String with both the first and last characters removed
str(2:end-1)
</lang>
 
Line 1,351:
 
<lang Prolog>remove_first_last_chars :-
L = "Rosetta",
L = [_|L1],
remove_last(L, L2),
remove_last(L1, L3),
writef('Original string : %s\n', [L]),
writef('Without first char : %s\n', [L1]),
writef('Without last char : %s\n', [L2]),
writef('Without first/last chars : %s\n', [L3]).
 
remove_last(L, LR) :-
append(LR, [_], L).</lang>
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  5, offsets from the end do not use negative numbers, but instead require a function expressing the negative offset relative to the length parameter, which is supplied by the operator. The form <tt>*-1</tt> is just a simple way to write such a function.
 
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]; # strip first character
puts [string range "write" 0 end-1]; # strip last character
puts [string range "brooms" 1 end-1]; # strip both first and last characters</lang>
 
=={{header|TorqueScript}}==
Line 1,878 ⟶ 1,928:
=={{header|VBScript}}==
<lang VBScript>Function TopNTail(s,mode)
Select Case mode
Case "top"
TopNTail = Mid(s,2,Len(s)-1)
Case "tail"
TopNTail = Mid(s,1,Len(s)-1)
Case "both"
TopNTail = Mid(s,2,Len(s)-2)
End Select
End Function
 
Anonymous user