- 积分
- 10789
- 明经币
- 个
- 注册时间
- 2006-8-1
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
初学.net,写了几个lisp字符串函数,解决lisp字符串处理功能较弱,请指点
using System;
using System.Text.RegularExpressions;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace commom_lib
{
/// <summary>
/// 辅助操作类
/// </summary>
public static partial class Tools
{
private static object results;
/// <summary>
/// 字符串替换,参数:被替换字符 旧字符 新字符
/// </summary>
/// <param name="rb"></param>
/// <returns></returns>
[LispFunction("zgx:str:replace")]
public static string Lsp_stringreplace(ResultBuffer rb)
{
string iRtn = null;
if (rb != null)
{
TypedValue[] tb = rb.AsArray();
if (tb.Length == 3 && tb[0].TypeCode == (int)LispDataType.Text && tb[1].TypeCode == (int)LispDataType.Text && tb[2].TypeCode == (int)LispDataType.Text)
{
string toreplace = tb[0].Value as string;
string oldstr = tb[1].Value as string;
string newstr = tb[2].Value as string;
iRtn = toreplace.Replace(oldstr, newstr);
}
}
return iRtn;
}
/// <summary>
/// 字符分割,同split,参数string seperator;
/// (zgx:str:Split input seperator)
/// </summary>
/// <param name="rb"></param>
/// <returns></returns>
[LispFunction("zgx:str:Split")]
public static ResultBuffer Lsp_stringSplit(ResultBuffer rb)
{
ResultBuffer reb = new ResultBuffer();
try
{
if (rb != null)
{
TypedValue[] tb = rb.AsArray();
//ResultBuffer reb = new ResultBuffer();
if (tb.Length == 2 && tb[0].TypeCode == (int)LispDataType.Text && tb[1].TypeCode == (int)LispDataType.Text)
{
string toReplace = tb[0].Value as string;
string sep = tb[1].Value as string;
string[] strArr = Regex.Split(toReplace, sep);
for (int i = 0; i < strArr.Length; i++)
{
if (strArr[i] != "")
reb.Add(new TypedValue((int)LispDataType.Text, strArr[i]));
}
}
else
reb = null;
}
else
reb = null;
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
}
//如何判断reb为空?
return reb;
}
/// <summary>
/// 正则表达式,与晓东函数顺序一致;
/// (zgx:regExps patten input [regoptions默认 1+2])
/// </summary>
/// <param name="rb"></param>
/// <returns></returns>
[LispFunction("zgx:regExps")]
public static ResultBuffer Lsp_regexps(ResultBuffer rb)
{
ResultBuffer Rsb = new ResultBuffer();
int RegOpts = 3;
try
{
if (rb != null)
{
TypedValue[] tb = rb.AsArray();
//ResultBuffer reb = new ResultBuffer();
if (tb.Length >= 2 & tb.Length <= 3 && tb[0].TypeCode == (int)LispDataType.Text && tb[1].TypeCode == (int)LispDataType.Text)
{
string pattern = tb[0].Value as string;
string s = tb[1].Value as string;
if (tb.Length == 3
&& tb[2].TypeCode == (int)LispDataType.Int32
| tb[2].TypeCode == (int)LispDataType.Int16
)
RegOpts = Convert.ToInt32(tb[2].Value);
MatchCollection results = Regex.Matches(s, pattern, (RegexOptions)RegOpts);
if (results.Count > 0)
for (int i = 0; i < results.Count; i++)
{
if (results[i].Value != "")
Rsb.Add(new TypedValue((int)LispDataType.Text, results[i].Value));
}
else
Rsb = null;
}
else
Rsb = null;
}
else
Rsb = null;
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
}
//如何判断reb为空?
return Rsb;
}
/// <summary>
/// (zgx:regExpr pat input new [替换选项 替换次数T,仅替换一次])
/// 0 None
///1 IgnoreCase
///2 Multiline
///4 ExplicitCapture
///8 Compiled
///16 Singleline
///32 IgnorePatternWhitespace
///64 RightToLeft
///256 ECMAScript
///512 CultureInvariant
/// </summary>
/// <param name="rb"></param>
/// <returns></returns>
[LispFunction("zgx:regExpr")]
public static string Lsp_regexpr(ResultBuffer rb)
{
string Rtnstr = null;
int RegOpts = 3;
try
{
if (rb != null)
{
TypedValue[] tb = rb.AsArray();
if (tb.Length >= 3 & tb.Length <= 5
&& tb[0].TypeCode == (int)LispDataType.Text
&& tb[1].TypeCode == (int)LispDataType.Text
&& tb[2].TypeCode == (int)LispDataType.Text
)
{
string pattern = tb[0].Value as string;
string sInput = tb[1].Value as string;
string sNew = tb[2].Value as string;
int iCount = sInput.Length;
if (tb.Length >= 4 & tb.Length <= 5
&& tb[3].TypeCode == (int)LispDataType.Int32
| tb[3].TypeCode == (int)LispDataType.Int16
)
RegOpts = Convert.ToInt32(tb[3].Value);
if (tb.Length == 5
&& tb[4].Value != null
)
iCount = 1;
Regex reg = new Regex(pattern, (RegexOptions)RegOpts);
Rtnstr = reg.Replace(sInput, sNew, iCount);
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
}
return Rtnstr;
}
//string input = "aaaaaaaaaaa";
//string pat = "a";
//string newstring = "v";
//Regex re = new Regex(pat);
//string rtn = re.Replace(input, newstring, input.Length, 1);
/// <summary>
/// substr同lispsubstr,参数:字符串 起始位置(从1开始) 截取长度
/// </summary>
/// <param name="rb"></param>
/// <returns></returns>
[LispFunction("zgx:str:Substring")]
public static string Lsp_stringSubstring(ResultBuffer rb)
{
string iRtn = null;
if (rb == null) return null;
try
{
TypedValue[] tb = rb.AsArray();
if (tb.Length == 3 &&
tb[0].TypeCode == (int)LispDataType.Text &&
tb[1].TypeCode == (int)LispDataType.Int32 |
tb[1].TypeCode == (int)LispDataType.Int16 |
tb[1].TypeCode == (int)LispDataType.Double &&
tb[2].TypeCode == (int)LispDataType.Double |
tb[2].TypeCode == (int)LispDataType.Int32 |
tb[2].TypeCode == (int)LispDataType.Int16)
{
string toReplace = tb[0].Value as string;
int strLen = toReplace.Length;
int start = Convert.ToInt32(tb[1].Value) - 1;
int Length = Convert.ToInt32(tb[2].Value);
if (start < 1)
start = 1;
if (start + Length > strLen)
Length = strLen - start;
iRtn = toReplace.Substring(start, Length);
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
}
return iRtn;
}
/// <summary>
/// 返回字符串长度,汉字按一个字符计算;
/// 输入:字符串
/// </summary>
/// <param name="rb"></param>
/// <returns></returns>
[LispFunction("zgx:str:length")]
public static object Lsp_strlength(ResultBuffer rb)
{
object iRtn = null;
if (rb == null) return null;
TypedValue[] tb = rb.AsArray();
try
{
if (tb.Length == 1 && tb[0].TypeCode == (int)LispDataType.Text)
{
string toreplace = tb[0].Value as string;
iRtn = toreplace.Length;
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
}
return iRtn;
}
}
}
|
|