Comparing and splitting of string inside JavaScript function - javascript

I have a string in a variable:
var test= "http://www.gmail.com#%#http://www.google.com#%#http://www.yahoo.com#%#";
I want to split this string at the occurrences of the special characters, ie: #%#, then after splitting I want to push this thing to an array like this:
var spcds = [];
spcds.push("http://www.gmail.com");
spcds.push("http://www.google.com");
spcds.push("http://www.yahoo.com");
What I need is just split the string variable and push that to the spcds array. How can I do this in my JavaScript function such that the resultant values will be stored to another variable which I then push to the array spcds.

Use the split method to split the string:
var parts = test.split("#%#");
And if you don’t want to have empty parts, use the filter method to filter out the values that are not an empty string:
var parts = test.split("#%#").filter(function(val){return val!="";});

This will give you an array of the strings you are after Then you can just iterate through the array as needed.
var mySplitResult = myString.split("#%#");

this should give you the array you want:
var testarray=
("http://www.gmail.com#%#http://www.google.com#%#http://www.yahoo.com#%#")
.split('#%#');

Related

Split a single array with respect to comma in javascript

I am getting an array of single string $scope.Obj= ["Talking Spanish,Spanish food,Spanish things,Football"];
The split should be by watching ,
I need to break it down = ["Talking Spanish","Spanish food","Spanish things","Football"];
How can I do it using javascript?
You can use split
let arr = ["Talking Spanish,Spanish food,Spanish things,Football"];
let op = arr[0].split(',')
console.log(op)
You can split 0th index of $scope.Obj with , and reassign to $scope.Obj.
$scope={};
$scope.Obj= ["Talking Spanish,Spanish food,Spanish things,Football"];
$scope.Obj=$scope.Obj[0].split(",")
console.log($scope.Obj);
str.split(separator, limit)
The above is common syntax to split a array
https://medium.freecodecamp.org/how-to-make-your-react-native-app-respond-gracefully-when-the-keyboard-pops-up-7442c1535580
The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
In the following example, split() looks for spaces in a string and returns the first 3 splits that it finds.
var myString = 'Hello World. How are you doing?';
var splits = myString.split(' ', 3);
console.log(splits);
This script displays the following:
["Hello", "World.", "How"]

Splitting String and taking out specific elements in node js

I have Sample String like this
"Organisation/Guest/images/guestImage.jpg"
I need to take out Organisation,Guest separately.
I have tried split() but can't get desired output.
var str = "Organisation/Guest/images/guestImage.jpg";
var res = str.split("/");
console.log(res[0]);
console.log(res[1]);
You can use of String.replace() along with regex
const regex = /Organisation\/|\/Organisation/;
console.log('Organisation/Guest/images/guestImage.jpg'.replace(regex, ''));
console.log('Guest/Organisation/images/guestImage.jpg'.replace(regex, ''));
console.log('Guest/images/guestImage.jpg/Organisation'.replace(regex, ''));
var yourString = "Organisation/Guest/images/guestImage.jpg";
yourString.split('/')
// this returns all the words in an array
yourString[0] // returns Organisation
yourString[1] // returns Guest and so on
When you run .split() on a string, it will return a new array with all the words in it. In the code I am splitting by the slash /
Then I save the new array in a variable. Now you should know we can access array properties like this: array[0] where 0 is the first index position or the first word, and so on.

JavaScript regular Expression to extract a substring which is in double qoutes, from a master string

i have a string , which look like an array of strings. I need a regex pattern in javascript to extract the substrings inside that master string, and return me the substring. Look at the string given below, and i need a regex on the basis of this string. And the regex will extract the substrings like :
files/file/img1.jpg ,files/file/img2.jpg and so on, until the end. As much directories i have , i want them to be extracted. For time being just consider that its not an array, its just a string. Thank you
str =
"["files/file/img1.jpg","files/file/img2.jpg","files/file/img3.jpg","files/file/img4.jpg","files/file/img5.jpg","files/file/img6.jpg"] ";
Use JSON.parse to parse your string.Then just iterate over the array.
var str =
'["files/file/img1.jpg","files/file/img2.jpg","files/file/img3.jpg","files/file/img4.jpg","files/file/img5.jpg","files/file/img6.jpg"]';
var result = JSON.parse(str);
for(var i= 0; i< result.length;i++){
console.log(result[i]);
}
The syntax is kinda wrong. It should be:
var str ="[\"files/file/img1.jpg\",\"files/file/img2.jpg\",\"files/file/img3.jpg\",\"files/file/img4.jpg\",\"files/file/img5.jpg\",\"files/file/img6.jpg\"] ";
Apart from that we can just use multiple Javascript string manipulation functions to extract the string:
data = str.split('["')[1].split('"]')[0].replace(/"/g, "").split(',');
Now the variable data is an array which has all the directories you need. The output looks something like this:
(6) ["files/file/img1.jpg", "files/file/img2.jpg", "files/file/img3.jpg", "files/file/img4.jpg", "files/file/img5.jpg", "files/file/img6.jpg"]
You can get each directory by indexing it: data[0] is the first one.

js - convert a whole array of strings into a string in which strings are separated by |

I have an array full of strings like [a,b,c,d]. I want to know the efficient way of converting this into 'a|b|c|d' using Javascript.
Thanks.
Pretty simple using Array.prototype.join()
var data = ['a','b','c','d'];
console.log(data.join('|'));
You can use array.join,
var pipe_delimited_= string_array.join("|");
DEMO
var string_array = ['a','b','c','d'];
var pipe_delimited = string_array.join("|");
console.log(pipe_delimited);
Try using array's join() method.The join() method joins array elements into a string.
var arr = ['a','b','c','d'];//your aray
var string =arr.join("|");
console.log(string);
For more see here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

How to convert a string into an array?

How do I convert the following string to an array?
String:
var string = "[energy,people,coding,vein]";
Array:
var array = ["energy","people","coding","vein"];
The easiest way to convert a string of comma separated values into an array is just to use split.
string1 = "testing,stuff";
arr = string1.split(","); // arr now equals ["testing", "stuff"]
The opening and closing brackets you could just get rid of using the substr method, or just leaving them off initially if you have that option
This is a two part process:
Remove the brackets with String.substr() (assuming the first and last characters in your string are brackets).
Use String.split() to break the string into an array.
This is the most efficient way to convert the string '[energy,people,coding,vein]' into an array:
var String1 = '[energy,people,coding,vein]',
array = String1.substr(1, String1.length - 2).split(',');
console.log(array); // ["energy", "people", "coding", "vein"]

Categories

Resources