C # Remove the first and last characters or strings of a string,

Source: Internet
Author: User

C # Remove the first and last characters or strings of a string,

When importing Excel attribute data to layer elements, Excel and SDE database data have an associated field, which is used to import matching attributes to the database.

In actual business, because the census data often has complex field values, you may simplify the data written to the layer. For example, if one element number is 0532BH001, it is possible that only BH001 is input to the layer. As a result, the values of the layer elements and the fields associated with the Excel file are not necessarily identical, and there may be some matching relationships.

In this case, the complexity is not considered, but the associated Field Values in Excel are the prefix, suffix, or prefix of the element associated field values.

Because it is cyclical Based on Excel, the field value is constructed and then the corresponding elements are searched through the Query operation in the layer, therefore, you cannot retrieve the field value in the layer element association field and then judge and process it by connecting the prefix and suffix. You can only intercept the associated field value in Excel.

According to the actual situation here, the format is relatively fixed. The functions for removing the prefix and suffix are as follows:

 

/// <Summary> /// remove the prefix string /// </summary> /// <param name = "val"> original string </param> /// <param name = "str"> prefix string </param> // <returns> </returns> private string GetRemovePrefixString (string val, string str) {string strRegex = @ "^ (" + str + ")"; return Regex. replace (val, strRegex ,"");} /// <summary> /// remove the suffix string /// </summary> /// <param name = "val"> original string </param> /// <param name = "str"> suffix string </param> // <returns> </returns> private string GetRemoveSuffixString (string val, string str) {string strRegex = @ "(" + str + ")" + "$"; return Regex. replace (val, strRegex ,"");}

 

If the string requirements are not so strict, but only the front and back strings are intercepted, you can do this:

 

/// <Summary> /// capture the front and back strings /// </summary> /// <param name = "val"> original string </param> /// <param name = "str"> string to be truncated </param> // <param name = "bAllStr"> whether to intercept the entire string // if true the entire string is intercepted. // if the value is false, only the prefix or suffix is intercepted. </param> /// <returns> </returns> private string GetString (string val, string str, bool bAllStr) {return Regex. replace (val, @ "(^ (" + str + ")" + (bAllStr? "*": "") + "| (" + Str + ")" + (bAllStr? "*": "") + "$ )","");}

 

To intercept a single character rather than a string, you can use the TrimStart or TrimEnd function to process it:

 

/// <Summary> /// capture the front and back characters /// </summary> /// <param name = "val"> original string </param> /// <param name = "c"> characters to be intercepted </param> // <returns> </returns> private string GetString (string val, char c) {return val. trimStart (c ). trimEnd (c );}

 

The above motion graphs are provided by "Graph dado ".

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.