在.net中如何检查输入的名称是否为有效字符串?

本文介绍了一种方法来检查用户提供的字符串是否适用于AutoCAD中的块名或图层名。通过使用SymbolUtilityServices.ValidateSymbolName()方法,可以有效验证名称是否符合规范。

How to check if a given string is a valid name for an item in a symbol table?

By Marat Mirgaleev

Issue

I want to check if a string provided by the user is suitable for a block name or for a layer name. Is there any function that can validate the string?

Solution

You are right, not every name is acceptable, for example, for a block. When creating a block in the AutoCAD UI, the user can get the following error message:


As you may know, there is a number of Symbol Tables in an dwg file, like Block table, Linetype table etc.

To check if a given string is a valid symbol table name or not, in ObjectARX you can use acdbSNValid() function.

AutoCAD .NET API provides the SymbolUtilityServices.ValidateSymbolName() for the same purpose. The method throws an exception when the name is invalid. Here is an example:

string[] names = { "lk3j4!@ #`$>%"// will cause an exception

                   "lk3j4!@ #$%" }; // will work fine

foreach (string s in names)

{

  try

  {

    // Validate the provided symbol table name

    SymbolUtilityServices.ValidateSymbolName( s, false);

 

    System.Windows.Forms.MessageBox.Show( s + " is a valid name." );

  }

  catch

  {

    // An exception has been thrown, indicating that

    //    the name is invalid

    System.Windows.Forms.MessageBox.Show( s +" is an invalid name.");

  }

// foreach

The 2nd parameter of the ValidateSymbolName() is a flag that allows or disallows the '|' symbol (so called "pipe" symbol).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值