从字符串创建string [] args。

编程入门 行业动态 更新时间:2024-10-26 19:30:53
本文介绍了从字符串创建string [] args。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个类,它在构造函数中接受程序主函数中的字符串[] args。我希望能够使用字符串创建实例。 你可能知道,它比仅仅拆分空格要复杂一点,因为空间是有效的当前面和后面跟着s。是否有一种简单的方法可以对此进行逆向工程,我没有想到? static void Main( string [] args) { if (args.length == 0 ) { string databasestatement = GetArgsFromDatabase(); string [] argsfromDB = DOSOMETHING(databasestatement); myclass fromDB = new myclass(argsfromDB); } else { myclass fromArgs = new myclass(args); } }

解决方案

Daniel Earwicker有一个非常好的实现 StackOverflow [ ^ ]:

public static string TrimMatchingQuotes( this string 输入, char 引用) { if ((输入.Length > = 2 )&& (输入[ 0 ] == quote)&&(输入[input.Length - 1 ] == quote)) return input.Substring( 1 ,input.Length - 2 ); return 输入; } public static IEnumerable< string>拆分(此 字符串 str,Func< char,bool>控制器) { int nextPiece = 0 ; for ( int c = 0 ; c < str.Length; c ++) { if (controller(str [c])) { yield return str.Substring(nextPiece,c - nextPiece); nextPiece = c + 1 ; } } yield return str。子(nextPiece); } public static IEnumerable< string> SplitCommandLine( string commandLine) { bool inQuotes = 假; return commandLine.Split(c = > { if (c == ' \ ')inQuotes =!inQuotes; return !inQuotes&& c == ' '; })。选择(arg = > arg.Trim()。TrimMatchingQuotes(' \'))。 Where(arg = > !string.IsNullOrEmpty(arg)); }

同一个帖子还包括 P / Invoke解决方案 [ ^ ]

I have a class that accepts the string[] args from the program's main function in its constructor. I would like to be able to create instances using a string as well. As you may know, it's a little more complicated than just splitting on spaces since space's are valid when preceded and followed by "s. Is there a simple way to reverse engineer this that I'm not thinking of?

static void Main(string[] args) { if (args.length == 0) { string databasestatement = GetArgsFromDatabase(); string[] argsfromDB = DOSOMETHING(databasestatement); myclass fromDB = new myclass(argsfromDB); } else { myclass fromArgs = new myclass(args); } }

解决方案

There's a pretty good implementation by Daniel Earwicker on StackOverflow[^]:

public static string TrimMatchingQuotes(this string input, char quote) { if ((input.Length >= 2) && (input[0] == quote) && (input[input.Length - 1] == quote)) return input.Substring(1, input.Length - 2); return input; } public static IEnumerable<string> Split(this string str, Func<char, bool> controller) { int nextPiece = 0; for (int c = 0; c < str.Length; c++) { if (controller(str[c])) { yield return str.Substring(nextPiece, c - nextPiece); nextPiece = c + 1; } } yield return str.Substring(nextPiece); } public static IEnumerable<string> SplitCommandLine(string commandLine) { bool inQuotes = false; return commandLine.Split(c => { if (c == '\"') inQuotes = !inQuotes; return !inQuotes && c == ' '; }) .Select(arg => arg.Trim().TrimMatchingQuotes('\"')) .Where(arg => !string.IsNullOrEmpty(arg)); }

The same thread also includes the P/Invoke solution[^].

更多推荐

从字符串创建string [] args。

本文发布于:2023-10-28 02:07:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535190.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   string   args

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!