任何人都可以帮助将这个JAVA代码转换为C#吗? [英] Can anyone help out in converting this JAVA code into C#?

查看:58
本文介绍了任何人都可以帮助将这个JAVA代码转换为C#吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C#并发现将此JAVA代码转换为C#的困难。如果有人提供解决方案。对我的理解会更好。



I'm learning C# and finding difficulties in converting this JAVA code into C#. If anyone would provide a solution. Would be better for my understanding.

public static List<Operator> oprs = new List<Operator>();
private List<String> GetTokens(String s)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(Pattern.quote(oprs[0].symbol));

        for (int i = 1; i < oprs.Count; i++)
        {
            sb.Append("|");
            sb.Append(Pattern.quote(oprs.get(i).symbol));
        }
        sb.Append("|[a-zA-Z_]+[a-zA-Z0-9_]*\\([^()]*\\)|[0-9]+|[a-zA-Z_][a-zA-Z0-9_]*|\\Q(\\E|\\Q)\\E");

        List<String> tokens = new List<String>();
        Pattern p = Pattern.compile(sb.ToString());
        Matcher m = p.matcher(s);

        while (m.find()) tokens.add(m.group());
        return tokens;
    }





我的尝试:



尝试1



What I have tried:

Try 1

public static List<operator> oprs = new List<operator>();
private List<string> GetTokens(String s)
    {
Dictionary<string,> dictionary = new Dictionary<string,>()
            {
                {source, " " }
            };
            var matches = dictionary.SelectMany(a => Regex.Matches(s, a.Value)
            .Cast<match>()
            .Select(b=>
                    new
                    {
                        Index = b.Index,
                        Value = b.Value,
                        Token = a.Key
                    }))
            .OrderBy(a => a.Value).ToList();
            List<string> tokens = new List<string>();
            foreach(var items in matches)
            {
                tokens.Add(items.Value.ToString());
            }
return tokens;
}





尝试2



Try 2

private List GetTokens(String s)
{
    List tokens = new List();
    Regex rx = new Regex(@"|[a-zA-Z_]+[a-zA-Z0-9_]*\\([^()]*\\)|[0-9]+|[a-zA-Z_][a-zA-Z0-9_]*|\\Q(\\E|\\Q)\\E");

    MatchCollection matches = rx.Matches(s);
    if (matches.Count > 0)
    {
        Match match = matches[0];
        GroupCollection groupCollection = match.Groups;
    }

    foreach(var ma in matches)
    {
        tokens.Add(ma.ToString());
    }

    return tokens;
}

推荐答案

如何使用其中一个转换器,然后研究并调整结果?



点击此处: [ 6个最好的java转换器]



请注意,varycode目前正在更新java算法,并且在线无法使用。





微软还提供此功能



[在VS中(参见MSDN)]
How about you use one of the converters and then study and tweak the result?

Check here: [6 best java converters]

Note that varycode is currently updating java algorithm and is unavailable online.


Microsoft also provides this functionality

[in VS (see MSDN)]


这篇关于任何人都可以帮助将这个JAVA代码转换为C#吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆