☰
d2jsp
Rules
Help
Forum Gold FAQ
Live Streams
Photo Gallery
Hourly Raffle
Ladder Slasher
Log In
Register
Log In
Register
Account Recovery
Resend Validation Email
d2jsp Forums
>
Off-Topic
>
Computers & IT
>
Programming & Development
> Javascript Help
Add Reply
New Topic
New Poll
Views: 510
Replies: 2
Track Topic
shenk
Member
Posts: 11,254
Joined: Nov 6 2004
Gold
:
0.00
#1
Mar 4 2017 11:07am
How can i take a string of words and replace / switch the first and last words with each other?
I.E.
original - this is a sentence
new - sentence is a the
carteblanche
Member
Posts: 32,925
Joined: Jul 23 2006
Gold
:
3,804.50
#2
Mar 4 2017 01:35pm
split the string into an array. swap the first and last entries.
lilpsychokilla
Member
Posts: 31,235
Joined: Nov 17 2007
Gold
:
27,290.00
#3
Mar 19 2017 12:40am
Code
function swapfirstandlast2(str){
var arr = str.split(" ");
var temp = arr[0]
arr[0] = arr[arr.length - 1];
arr[arr.length -1] = temp;
var newstr = arr.join(" ");
return newstr
}
This post was edited by lilpsychokilla on Mar 19 2017 01:00am
Go Back To
Programming & Development
Topic List
Add Reply
New Topic
New Poll
© 2003-2024 d2jsp
Contact