Example:
let somestr = '11>22>33>44';
let someSpecificWord = '22';
I want to get a result like this
'11>22'
How to cut or use method for this?
You may use String#substring with String#lastIndexOf:
let somestr = '11>22>33>44';
let someSpecificWord = '22';
console.log(somestr.substring(0, somestr.lastIndexOf(someSpecificWord) + someSpecificWord.length));
In case u want all of the numbers included in result.
var str = '11>22>33>44';
var splitstr = str.split('>');
var strarray =[];
for(var i=0; i<splitstr.length-1;i++){
strarray[i] = splitstr[i]+'>'+splitstr[i+1];
$("span").append(strarray[i]+"<br />");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="span"></span>
You can also split() the string to array and then find the indexOf() the work, then join() using > to get the final result. For better code management, create a reusable function.
function cutWord(str, word){
var splitStr = str.split('>');
return splitStr.slice(0,splitStr.lastIndexOf(someSpecificWord)+1).join('>');
}
var somestr = '11>22>33>44';
var someSpecificWord = '22';
console.log(cutWord(somestr, someSpecificWord));
someSpecificWord = '33';
console.log(cutWord(somestr, someSpecificWord));
someSpecificWord = '11';
console.log(cutWord(somestr, someSpecificWord));
Related
hello I have values like this
1-10
2-3
901-321
I want to get the reverse values for example like this
10-1
3-2
321-901
I have tried this
var str = "1-18";
var newString = "";
for (var i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
But it gives me 81-1
Instead, use String.split(), Arrary.reverse() and Arrary.join():
var str = '901-321';
var strArray = str.split('-'); // ['901', '321']
var strArrayReversed = strArray.reverse(); // ['321', '901']
var result = strArrayReversed.join('-'); // '321-901'
console.log('result = ', result);
// You can do all these steps above in one go as:
var result2 = str.split('-')
.reverse()
.join('-');
console.log('result2 = ', result2);
MDN Docs:
String.prototype.split()
Array.prototype.reverse()
Array.prototype.join()
Can split on the - to create array , reverse the array and join it back into string
var str = "1-18",
newStr = str.split('-').reverse().join('-');
console.log(newStr)
a = "12-5"
console.log(a.split('-').reverse().join('-'))
You can use the split method to divide the string in two, and then use the second part before the first, like this:
var str = "1-18";
var l = str.split("-");
return l[1] + "-" + l[0];
You could replace the string by swapping the values.
var string = '901-321';
console.log(string.replace(/(.+)-(.+)/, '$2-$1'));
Using JavaScript I want to take a string like this var hashStr = 'modal-123456' and assign the string left of the - to a variable and the string right of the - to another variable.
If the string does not contain a - then ignore it.
How can I best achieve this?
var hashStr = location.hash.replace('#', '');
// hashStr now === 'modal-123456'
var firstHalf = // modal
var secondHalf = // '123456'
You can use split API.
var hashStr = 'modal-123456'
var splitStr = hashStr.split('-');
console.log(splitStr[0])
console.log(splitStr[1])
Just use split.
var hashStr = 'modal-123456';
var [firstHalf, secondHalf] = hashStr.split("-");
console.log("first half:", firstHalf);
console.log("second half:", secondHalf);
Simply
var hashStr = location.hash.replace('#', '');
var firstHalf = hashStr.split("-")[0];
var secondHalf = hashStr.split("-")[1];
or
var hashStr = location.hash.replace('#', '').split("-");
var firstHalf = hashStr[0];
var secondHalf = hashStr[1];
I have data two sets of data as follows:
"One.Two.Three.Four"
"One.Two.Three.1.Four"
The first three parts are fixed and the remaining can extend to as many as possible.
I am trying to build an object where I want to split and combine whatever is present after three into an object.
var split = samplestr.split('.');
var finalarray = [];
if(split.length>4)
{
finalarray[0] = split[0];
finalarray[1] = split[1];
finalarray[2] = split[2];
finalarray[3] = split[3]+"."split[4];
}
I need to generalise this such that even if the string is of the form
"One.Two.Three.1.2.3.Four"
finalarray[3] = 1.2.3.Four;
Any hints on generalising this?
With Array#shift and Array#join.
var split = samplestr.split('.');
var finalarray = [];
if(split.length > 4) {
finalarray[0] = split.shift();
finalarray[1] = split.shift();
finalarray[2] = split.shift();
finalarray[3] = split.join(".");
}
simply replace
finalarray[3] = split[3]+"."split[4];
with
finalarray[3] = split.slice(3).join(".");
Split the string, slice the first part and append the join'ed second part:
console.info=function(x){document.write('<pre>'+JSON.stringify(x,0,3)+'</pre>')}
//--
var str = "One.Two.Three.Four.More.Stuff";
var s = str.split('.');
var result = s.slice(0, 3).concat(s.slice(3).join('.'));
console.info(result);
I have a String "SHELF-2-1-1-2-1", I need to remove "2" from that string and want the output to be "SHELF-1-1-2-1"
I tried:
var str = "SHELF-2-1-1-2-1";
var res = str.split("-");
How can I join the array to get "SHELF-1-1-2-1"?
This would work:
var str = "SHELF-2-1-1".split('-2').join('');
Sounds like you want to do a replace... Try:
var res = str.replace('-2', '');
var str = "SHELF-2-1-1";
var res = str.split("-");
res.pop(res.indexOf('2'));
var newStr = res.join('-');
This should also work for your updated question, as it will only remove the first 2 from the string
let str = "Hello India";
let split_str = str.split("");
console.log(split_str);
let join_arr = split_str.join("");
console.log(join_arr);
I have a string for e.g:
var str = 'abcdef';
i want to get it in an array format e.g:
var a;
a[0]=a;
a[1]=b; and so on..
i am aware of split method but in this case of string without any space how can i split as individual character??
Use: str.split(''). That will create an array with characters of str as elements.
You can access it like this ,
var str='abcdef';
alert(str[0]);
or
var str='abcdef';
alert(str.charAt(0));
refer following link to chose which is the best way.
http://blog.vjeux.com/2009/javascript/dangerous-bracket-notation-for-strings.html
var s = "abcdef";
var a;
for (var i = 0; i < s.length; i++) {
a.push(s.charAt(i));
}
or
var s = "abcdef";
var a;
var a= s.split('');
Try using this code:-
var array = str.split('');
Look at the following page to know more about splits.
Javascript split
var str="abcdef";
var a=new Array;
for (var x=0; x<str.length; x++) {
a[x] = str.charAt(x);
}
console.log(a);