.net 定义lisp字符串函数
初学.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>
public static string Lsp_stringreplace(ResultBuffer rb)
{
string iRtn = null;
if (rb != null)
{
TypedValue[] tb = rb.AsArray();
if (tb.Length == 3 && tb.TypeCode == (int)LispDataType.Text && tb.TypeCode == (int)LispDataType.Text && tb.TypeCode == (int)LispDataType.Text)
{
string toreplace = tb.Value as string;
string oldstr = tb.Value as string;
string newstr = tb.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>
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.TypeCode == (int)LispDataType.Text && tb.TypeCode == (int)LispDataType.Text)
{
string toReplace = tb.Value as string;
string sep = tb.Value as string;
string[] strArr = Regex.Split(toReplace, sep);
for (int i = 0; i < strArr.Length; i++)
{
if (strArr != "")
reb.Add(new TypedValue((int)LispDataType.Text, strArr));
}
}
else
reb = null;
}
else
reb = null;
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
}
//如何判断reb为空?
return reb;
}
/// <summary>
/// 正则表达式,与晓东函数顺序一致;
/// (zgx:regExps patten input )
/// </summary>
/// <param name="rb"></param>
/// <returns></returns>
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.TypeCode == (int)LispDataType.Text && tb.TypeCode == (int)LispDataType.Text)
{
string pattern = tb.Value as string;
string s = tb.Value as string;
if (tb.Length == 3
&& tb.TypeCode == (int)LispDataType.Int32
| tb.TypeCode == (int)LispDataType.Int16
)
RegOpts = Convert.ToInt32(tb.Value);
MatchCollection results = Regex.Matches(s, pattern, (RegexOptions)RegOpts);
if (results.Count > 0)
for (int i = 0; i < results.Count; i++)
{
if (results.Value != "")
Rsb.Add(new TypedValue((int)LispDataType.Text, results.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>
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.TypeCode == (int)LispDataType.Text
&& tb.TypeCode == (int)LispDataType.Text
&& tb.TypeCode == (int)LispDataType.Text
)
{
string pattern = tb.Value as string;
string sInput = tb.Value as string;
string sNew = tb.Value as string;
int iCount = sInput.Length;
if (tb.Length >= 4 & tb.Length <= 5
&& tb.TypeCode == (int)LispDataType.Int32
| tb.TypeCode == (int)LispDataType.Int16
)
RegOpts = Convert.ToInt32(tb.Value);
if (tb.Length == 5
&& tb.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>
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.TypeCode == (int)LispDataType.Text &&
tb.TypeCode == (int)LispDataType.Int32 |
tb.TypeCode == (int)LispDataType.Int16 |
tb.TypeCode == (int)LispDataType.Double &&
tb.TypeCode == (int)LispDataType.Double |
tb.TypeCode == (int)LispDataType.Int32 |
tb.TypeCode == (int)LispDataType.Int16)
{
string toReplace = tb.Value as string;
int strLen = toReplace.Length;
int start = Convert.ToInt32(tb.Value) - 1;
int Length = Convert.ToInt32(tb.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>
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.TypeCode == (int)LispDataType.Text)
{
string toreplace = tb.Value as string;
iRtn = toreplace.Length;
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
}
return iRtn;
}
}
}
页:
[1]