Split and join in JavaScript? - javascript

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);

Related

How to cut a string in this case in JavaScript?

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));

Javascript regex get string before second /

var str = '#/promotionalMailer/test1';
output should be ==> #/promotionalMailer
I want the string before the second slash '/'
I have tried this so far:
var str = '#/promotionalMailer/test1';
var match = str.match(/([^\/]*\/){2}/)[0];
alert(match);
But it comes with the second slash.
try split, slice and join
var str = '#/promotionalMailer/test1';
console.log( str.split("/").slice(0,2).join("/"));
For example,
var str = '#/promotionalMailer/test1/foo/bar/baz';
result = str.split('/').slice(0, 2).join('/')
document.write('<pre>'+JSON.stringify(result,0,3));
If you want regexes, then
var str = '#/promotionalMailer/test1/foo/bar/baz';
result = str.match(/[^\/]*\/[^\/]*/)[0]
document.write('<pre>'+JSON.stringify(result,0,3));

How to get particular string from one big string ?

I have string
var str = "Ahora MXN$1,709.05" and wanted to get only
"MXN$1,709.05" from this.
Can someone please help me?
You can use substring or replace. With replace you are going to replace something with nothing.
replace
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.replace(sub,'');
substring
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.substring(sub.length);
JsFiddle
You can use either substring or Regex
Using substring
var str = "Ahora MXN$1,709.05";
var result = str.substring('Ahora '.length);
console.log(result);
Using Regex
var str = "Ahora MXN$1,709.05";
var myRegexp = /Ahora\s(.*?)(?:\s|$)/g;
var match = myRegexp.exec(str);
console.log(match[1]);

JavaScript get character in sting after [ and before ]

I have some strings like:
str1 = "Point[A,B]"
str2 = "Segment[A,B]"
str3 = "Circle[C,D]"
str4 = "Point[Q,L]"
Now I want to have function that gives me character after "[" and the character before "]". How could I make something like that ?
try this one...
var str = "Point[A,B]";
var start_pos = str.indexOf('[') + 1;
var end_pos = str.indexOf(']',start_pos);
var text_to_get = str.substring(start_pos,end_pos)
alert(text_to_get);
You'd need regex to do that
var matches = /\[(.*?)\]/.exec(str1);
alert(matches[1]);
You can use match() to extract the characters:
str.match(/\[(.*)\]/)[1]
A safer way would be:
var matches = str.match(/\[(.*)\]/);
if(matches) {
var chars = matches[1];
}
Here's an approach which avoids regex.
var str = "Point[A,B]";
var afterOpenBracket = str.split("[")[1]; // returns "A,B]"
var bracketContents = afterOpenBracket.split("]")[0]; // returns "A,B"
There, pretty simple! bracketContents now contains the entirety of the text between the first set of brackets.
We can stop here, but I'll go a step further anyway and split up the parameters.
var parameters = bracketContents.split(","); // returns ["A", "B"]
Or in case u have more [A,C,D,B] and don't want to use regex:
var str1 = "Point[A,C,D,B]";
function extract(str1){
var a = str1.charAt(str1.indexOf('[')+1);
var b = str1.charAt(str1.indexOf(']')-1);
return [a, b];
//or
//a.concat(b); //to get a string with that values
}
console.log(extract(str1));

Splitting in string in JavaScript

How to split a string in JavaScript with the "," as seperator?
var splitString = yourstring.split(',');
See split
var str = "test,test1,test2";
var arrStr = str.split(',');
var arrLength = arrStr.length; //returns 3
Use split to split your string:
"foo,bar,baz".split(",") // returns ["foo","bar","baz"]
var expression = "h,e,l,l,o";
var tokens = expression.split("\,");
alert(tokens[0]);// will return h

Categories