This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
- Clojure
- C#
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Go
- Haskell
- JS
- JS
- Java
- Lua
- PHP
- Pascal
- Pascal
- Pascal
- Perl
- Python
- Ruby
- Rust
- Rust
- VB
character(len=:), allocatable :: s1
character (len=:), allocatable :: s2
character(len=:), allocatable :: w
integer :: i, j, k, tc
allocate (s2, mold=s1)
i = 1
j = 1
do
k = index (s1(i:), w)
if (k == 0) then
s2(j:j+len(s1)-i) = s1(i:)
s2 = s2(:j+len(s1)-i)
exit
else
tc = k - 1
if (tc > 0) s2(j:j+tc-1) = s1(i:i+tc-1)
i = i + tc + len(w)
j = j + tc
end if
end do
Because s2 can only be shorter than s1, it is allocated with the size of s1 and shortened to its actual length with the "s2 = s2(:j+len(s1)-i)" statement.
remove :: String -> String -> String
remove w "" = ""
remove w s@(c:cs)
| w `isPrefixOf` s = remove w (drop (length w) s)
| otherwise = c : remove w cs
s2 = remove w s1
Instead of just doing the removal, define a function that does it. A function can be tested and reused.
var regex = RegExp(w, 'g');
var s2 = s1.replace(regex, '');
Search pattern can also be defined in Regular Expressions. See the documentation.
If a string is used instead of regex, only the first match will be replaced.
If a string is used instead of regex, only the first match will be replaced.
const s2 = s1.split(w).join('')
Better not to use a RegExp, in case the word contains dots, asterisks, &c. One may also wish to remove redundant spaces afterward: str.replace(/\s+/g, ' ')