Substring/Top and tail: Difference between revisions

Content added Content deleted
(Add source for Rust)
Line 616: Line 616:
end function
end function


puts(1, strip_first("knight")) -- strip first character
puts(1, strip_first("knight")) -- strip first character
puts(1, strip_last("write")) -- strip last character
puts(1, strip_last("write")) -- strip last character
puts(1, strip_both("brooms")) -- strip both first and last characters</lang>
puts(1, strip_both("brooms")) -- strip both first and last characters</lang>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
Line 1,088: Line 1,088:
<lang MATLAB>
<lang MATLAB>
% String with first character removed
% String with first character removed
str(2:end)
str(2:end)
% String with last character removed
% String with last character removed
str(1:end-1)
str(1:end-1)
% String with both the first and last characters removed
% String with both the first and last characters removed
str(2:end-1)
str(2:end-1)
</lang>
</lang>


Line 1,351: Line 1,351:


<lang Prolog>remove_first_last_chars :-
<lang Prolog>remove_first_last_chars :-
L = "Rosetta",
L = "Rosetta",
L = [_|L1],
L = [_|L1],
remove_last(L, L2),
remove_last(L, L2),
remove_last(L1, L3),
remove_last(L1, L3),
writef('Original string : %s\n', [L]),
writef('Original string : %s\n', [L]),
writef('Without first char : %s\n', [L1]),
writef('Without first char : %s\n', [L1]),
writef('Without last char : %s\n', [L2]),
writef('Without last char : %s\n', [L2]),
writef('Without first/last chars : %s\n', [L3]).
writef('Without first/last chars : %s\n', [L3]).


remove_last(L, LR) :-
remove_last(L, LR) :-
append(LR, [_], L).</lang>
append(LR, [_], L).</lang>
Output :
Output :
<pre> ?- remove_first_last_chars.
<pre> ?- remove_first_last_chars.
Line 1,476: Line 1,476:
(formerly Perl 6)
(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.
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.
We use musical sharps and flats to illustrate that Raku is comfortable with characters from any Unicode plane.
Line 1,618: Line 1,618:
print left$(s$,len(s$) -1) 'strip last
print left$(s$,len(s$) -1) 'strip last
print mid$(s$,2,len(s$) -2) 'strip first and last</lang>
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}}==
=={{header|Scala}}==
Line 1,693: Line 1,743:
<lang smalltalk>
<lang smalltalk>
s := 'upraisers'.
s := 'upraisers'.
Transcript show: 'Top: ', s allButLast; nl.
Transcript show: 'Top: ', s allButLast; nl.
Transcript show: 'Tail: ', s allButFirst; nl.
Transcript show: 'Tail: ', s allButFirst; nl.
Transcript show: 'Without both: ', s allButFirst allButLast; nl.
Transcript show: 'Without both: ', s allButFirst allButLast; nl.
Transcript show: 'Without both using substring method: ', (s copyFrom: 2 to: s size - 1); nl.
Transcript show: 'Without both using substring method: ', (s copyFrom: 2 to: s size - 1); nl.
</lang>
</lang>
Line 1,803: Line 1,853:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>puts [string range "knight" 1 end]; # strip first character
<lang tcl>puts [string range "knight" 1 end]; # strip first character
puts [string range "write" 0 end-1]; # strip last 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>
puts [string range "brooms" 1 end-1]; # strip both first and last characters</lang>


=={{header|TorqueScript}}==
=={{header|TorqueScript}}==
Line 1,878: Line 1,928:
=={{header|VBScript}}==
=={{header|VBScript}}==
<lang VBScript>Function TopNTail(s,mode)
<lang VBScript>Function TopNTail(s,mode)
Select Case mode
Select Case mode
Case "top"
Case "top"
TopNTail = Mid(s,2,Len(s)-1)
TopNTail = Mid(s,2,Len(s)-1)
Case "tail"
Case "tail"
TopNTail = Mid(s,1,Len(s)-1)
TopNTail = Mid(s,1,Len(s)-1)
Case "both"
Case "both"
TopNTail = Mid(s,2,Len(s)-2)
TopNTail = Mid(s,2,Len(s)-2)
End Select
End Select
End Function
End Function