I am stuck on a very annoying problem. Here is what I am trying to achieve. I read the latitude and longitude in two text boxes and then split each of the pair on a comma as that is what they are separated by. Then I need to parse them and create a LatLng object to create a Google marker. My problem for some reason is splitting the string. I know that all I need to do is use String.split() method to achieve it. Here is what I do:
Lets say the value in text box is 26.2338, 81.2336
//Reading the value in text boxes on HTML form
var sourceLocation =document.getElementById("source").value;
//Remove any spaces in between coordinates
var newString =sourceLocation.replace(/\s/g, '');
//Split the string on ,
newString.split(",");
//Creating latitude longitude objects of the source and destination
var newLoc =new google.maps.LatLng(parseFloat(newString[0]),parseFloat(newString[1]));
Now I am unable to understand as to why newString[0] just gives me 2 while it should give 26.2338. Similarly, newString[1] gives 6 instead of 81.2336. What am I doing wrong?? Any help will be greatly appreciated.
String.split() returns an array, it doesn't modify the string to somehow make it into an array. You want
var parts = newString.split(",");
var newLoc = new google.maps.LatLng(parseFloat(parts[0]),parseFloat(parts[1]));
Related
I have a string similar to below
"OPR_NAME:CODE=value,:DESC=value,:NUMBER=value,:INITIATOR=value,:RESP"
I am using StringTokenizer to split the string into tokens based on the delimiter(,:),I need the values of
CODE,DESC and NUMBER.
Can someone pls tell how to achieve this ? The values may come in random order in my string
For eg my string may be like below as well :
"OPR_NAME:DESC=value,:NUMBER=value,:CODE=value,:INITIATOR=value,:RESP" and still it should be able to fetch the values.
I did below to split the string into tokens
StringTokenizer st = new StringTokenizer(str,",:");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
But not sure how to store these tokens to just get the value of 3 fields as mentioned above.
Thanks !!
Okay so what I meant is, detect where is the "=" and then apply a substring to get the value you want.
rough example
System.out.println(st.nextToken().substring(st.nextToken().indexOf('=')+1,st.nextToken().length()));
Use split instead :
String[] parts = X.split(",:");
for (String x:parts) {
System.out.println(x.substring(x.indexOf('=')+1));
}
I am wondering is there any way to replace this type of value in string
https://farm5.staticflickr.com/4796/39790122335_bdc207b259_o.jpg https://farm5.staticflickr.com/4776/39790122225_c8e96339fa.jpg
What i want is that replace the right side of URL and just show the left side there is little space between them.
You can do that in many different ways, using many different languages.
For example, this is how you can do it with JavaScript
var str = 'https://farm5.staticflickr.com/4796/39790122335_bdc207b259_o.jpg https://farm5.staticflickr.com/4776/39790122225_c8e96339fa.jpg'
str = str.split(' ')
str = str[0]
it can be easily done using the split function.
First assign the url to a Variable
Python
theurl = 'https://farm5.staticflickr.com/4796/39790122335_bdc207b259_o.jpg https://farm5.staticflickr.com/4776/39790122225_c8e96339fa.jpg'
each_url = theurl.split()
now your new variable each url is a list of objects containing all the URLS
i.e each_url = ['https://farm5.staticflickr.com/4796/39790122335_bdc207b259_o.jpg ', 'https://farm5.staticflickr.com/4776/39790122225_c8e96339fa.jpg']
the Split function works in pretty different Programming Languages
though in some like Javascript you might have to specify split as split(' ')
showing you are splitting by spaces
what is the proper way to implement an autocomplete search with undescore?
i have a simple array (cities) and a text input field ($.autocomplete). when the user enters the first letters in the auto-complete textfield, it should output an array with all the cities starting with the entered letters (term).
cities:
["Graz","Hamburg","Innsbruck","Linz","München","Other","Salzburg","Wien"]
eventlistener:
$.autocomplete.addEventListener("change", function(e){
var cities = cities_array;
var term = $.autocomplete.value;
var results = _.filter(cities, function (city){
return
});
console.log(results + "this is results");
});
I’ve tried it with _.contains, but it only returns the city when its a complete match (e.g Graz is only output when „Graz“ is entered but not when „Gr“ is entered).
the _.filter/._select collection at http://underscorejs.org/docs/underscore.html are not very clear for me and the closest i found here is filtering JSON using underscore.
but i don’t understand the indexOf part.
thx for any suggestions!
By using #filter and #indexOf, you can get quite close to a pretty decent autocomplete.
What #indexOf does is that it checks the string if it contains the inputVal.
If it does not contain it it'll return -1 therefore our predicate below will work without fail.
Another small trick here is that you (read I) wanted it to be possible to search for s and get a hit for Innsbruck and Salzburg alike therefore I threw in #toLowerCase so that you always search in lower case.
return _.filter(cities, function(city) {
return city.toLowerCase().indexOf(inputVal.toLowerCase()) >= 0;
});
Ok, so I'm creating a Flash HUD in AS2 that runs on the Surface and connects to our server.
As it stands now, I'm having to hard code the IP addresses for the Surface to connect to, and I'm trying to get past this.
I have 4 text fields for the user to enter the 4 fields of IP address data. My issue at the moment is that if I set the String variable literally, it works fine. But if I dynamically create the string, instead of outputting on one line, it outputs each of the 4 strings separately.
Here's my code:
var newIP1 = getIP.IPtext.IP1.text; //grabbing the data from the UI
var newIP2 = getIP.IPtext.IP2.text;
var newIP3 = getIP.IPtext.IP3.text;
var newIP4 = getIP.IPtext.IP4.text;
var ipArray = new Array(newIP1,newIP2,newIP3,newIP4); //setting the array
trace (ipArray.join(".")); // output the string, replacing the commas with a period
//output:
//10
//.255
//.255
//.22
//If I do this it works fine
var IPstr = "10.255.255.2";
trace(IPstr);
// output: 10.255.255.22
I appreciate any help on this, thanks in advance.
Your code looks good and should work as expected.
One thing to check would be to see if there isn't a carriage return or newline character being added to each individual input box. One way to check would be to check the length of each of your input strings to ensure there isn't an invisible character there.
I am currently working on a project that dynamically displays DB content into table.
To edit the table contents i am want to use the dynamically created "string"+id value.
Is there any way to retrieve the appended int value from the whole string in javaScript?
Any suggestions would be appreciative...
Thanks!!!
If you know that the string part is only going to consist of letters or non-numeric characters, you could use a regular expression:
var str = "something123"
var id = str.replace(/^[^\d]+/i, "");
If it can consist of numbers as well, then things get complicated unless you can ensure that string always ends with a non-numeric character. In which case, you can do something like this:
var str = "something123"
var id = str.match(/\d+$/) ? str.match(/\d+$/)[0] : "";
(''+string.match(/\d+/) || '')
Explanation: match all digits in the variable string, and make a string from it (''+).
If there is no match, it would return null, but thanks to || '', it will always be a string.
You might try using the regex:
/\d+$/
to retrieve the appended number