首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何存储字符串值,如下面的数组值,而不用括号,用逗号分隔为延迟和经度

如何存储字符串值,如下面的数组值,而不用括号,用逗号分隔为延迟和经度
EN

Stack Overflow用户
提问于 2015-12-10 06:02:05
回答 3查看 61关注 0票数 0
代码语言:javascript
复制
 string locationPoints= "(29.67850809103362, 79.74288940429688),(29.367814384775375, 79.29519653320312),(29.561512529746768, 79.20455932617188),(29.69759650228319, 79.45449829101562)"; /* C# code */

    success: function (data) {   /* javascript code
                var points = [];
                points[0] = [25.1420, 55.1861];
                points[1] = [25.001225, 55.657674];
                points[2] = [25.3995, 55.4796];          .... }

我将c#方法的上述定位点传递给jqueryajax成功函数。

代码语言:javascript
复制
 I need to pass the locationPoints to javascriptMVCArray to draw polygon on map. here i have to store those locationPoints same as above points array. But i don't know that how to store. Please can anyone help me in that.?
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-10 06:41:48

所以你得用绳子弹奏一下。用逗号分隔,去掉额外的括号等。

代码语言:javascript
复制
 var locationPoints= "(29.67850809103362, 79.74288940429688),(29.367814384775375, 79.29519653320312),(29.561512529746768, 79.20455932617188),(29.69759650228319, 79.45449829101562)";

 var getPoints = function(){
   var pairs = locationPoints.split('),');
   var orderedPairs = [];
   pairs[pairs.length-1] = pairs[pairs.length-1].slice(0,-1);
   pairs.forEach(function(pair, index, array){
     array[index] = pair.substring(1);
     var orderedPair = array[index].split(',');
     orderedPairs.push([parseFloat(orderedPair[0]), parseFloat(orderedPair[1])]);
   });

   orderedPairs.forEach(function(pair, index, arr){
      $('#points').append('[' + pair[0] + ', ' + pair[1] + ']<br>');  
   });
 }

这里有一个与它有关的http://plnkr.co/edit/eYEwm84f5By40oYNpHNo?p=preview

票数 0
EN

Stack Overflow用户

发布于 2015-12-10 06:16:53

只是不要在你不需要的地方使用Regex。这里不是你需要Regex的地方,所以不要去那里。总结和澄清:不要为此使用正则表达式。

一些分割和修剪将完成同样的工作,速度要快得多:

代码语言:javascript
复制
                        // First we split by "),(" to get each of the points
var res = locationPoints.Splits(new[]{"),("})

                        // Each point-string will be handled to produce Lat and Lng
                        .Select(pt =>
                               {
                                   // Remove '(' and ')' to clean first/last points
                                   // And split by comma to get the two components
                                   var s = pt.Trim('()').Split(',');

                                   var lat = s[0].Trim();
                                   var lng = s[1].Trim();

                                   // Choose how to represent each point, example:
                                   return new LatLng {Lat=lat, Lng=lng};
                               })
                        // Make an array out of that
                        .ToArray();
票数 0
EN

Stack Overflow用户

发布于 2015-12-10 07:07:52

使用regex匹配,我们用简单的代码得到解决方案,

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace testpro1
{
    class Program
    {
       public string[] poarr = null;

        static void Main(string[] args)
        {
            string strRegex = @"([0-9]*\.[0-9]+|[0-9]+)";
            string strTargetString = @"(29.67850809103362, 79.74288940429688),(29.367814384775375, 79.29519653320312),(29.561512529746768, 79.20455932617188),(29.69759650228319, 79.45449829101562)";
            //matches is the array
            Match[] matches = Regex.Matches(strTargetString, strRegex)
                         .Cast<Match>()
                         .ToArray();
            Console.WriteLine("The Array Values are:\n");
            foreach (var item in matches)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadLine();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34194783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档