String Manipulation class

Source: Internet
Author: User

String Manipulation class
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

<summary>
format string
</summary>
public class Stringutil
{
Public Stringutil ()
{
//
TODO: Add constructor logic here
//
}
<summary>
Deletes the specified string from the tail in the string
</summary>
<param name= "Sourcestring" ></param>
<param name= "Removedstring" ></param>
<returns></returns>
public static string Remove (String sourcestring,string removedstring)
{
Try
{
if (Sourcestring.indexof (removedstring) <0)
throw new Exception ("The original string does not contain a remove string. ");
string result = sourcestring;
int lengthofsourcestring = Sourcestring.length;
int lengthofremovedstring = Removedstring.length;
int startIndex = lengthofsourcestring-lengthofremovedstring;
String tempsubstring = Sourcestring.substring (StartIndex);
if (tempsubstring.toupper () = = Removedstring.toupper ())
{
result = Sourcestring.remove (startindex,lengthofremovedstring);
}
return result;
}
Catch
{
return sourcestring;
}
}

<summary>
Get the string to the right of the split break
</summary>
<param name= "Sourcestring" ></param>
<param name= "Splitchar" ></param>
<returns></returns>
public static string Rightsplit (String sourcestring, char Splitchar)
{
String Result=null;
string[] tempstring = Sourcestring.split (Splitchar);
if (Tempstring.length >0)
{
result = Tempstring[tempstring.length-1]. ToString ();
}
return result;
}

<summary>
Get the string to the left of the split break
</summary>
<param name= "Sourcestring" ></param>
<param name= "Splitchar" ></param>
<returns></returns>
public static string Leftsplit (String sourcestring, char Splitchar)
{
String Result=null;
string[] tempstring = Sourcestring.split (Splitchar);
if (Tempstring.length >0)
{
result = Tempstring[0]. ToString ();
}
return result;
}

<summary>
Remove the last comma
</summary>
<param Name= "origin" ></param>
<returns></returns>
public static string Dellastcomma (String origin)
{
if (Origin. IndexOf (",") = =-1)
{
return origin;
}
return origin. Substring (0,origin. LastIndexOf (","));
}

  ///<summary>
  ///delete invisible characters
  ///</summary>
   <param name= "sourcestring" ></PARAM>
  ///<returns></returns>
   public static string Deleteunvisiblechar (String sourcestring)
  {
    System.Text.StringBuilder Sbuilder = new System.Text.StringBuilder (131);
   for (int i = 0;i < sourcestring.length; i++)
   {
     int Unicode = Sourcestring[i];
    if (Unicode >=)
    {
      Sbuilder.append (Sourcestring[i]. ToString ());
    }    
   }
   return Sbuilder.tostring ();
  }

<summary>
Gets the merged string for an array element
</summary>
<param name= "Stringarray" ></param>
<returns></returns>
public static string getarraystring (string[] stringarray)
{
string totalstring = null;
for (int i=0;i<stringarray.length;i++)
{
totalstring = totalstring + stringarray[i];
}
return totalstring;
}

<summary>
Gets the number of times a string appears in a string array
</summary>
<param name= "Stringarray" type= "string[]" >
<para>
///
</para>
</param>
<param name= "findstring" type= "string" >
<para>
///
</para>
</param>
<returns>
A int Value ...
</returns>
public static int Getstringcount (string[] stringarray,string findstring)
{
int count =-1;
String totalstring = Getarraystring (Stringarray);
string subString = totalstring;

while (Substring.indexof (findstring) >=0)
{
subString = totalstring.substring (Substring.indexof (findstring));
Count + 1;
}
return count;
}

<summary>
Gets the number of times a string appears in a string
</summary>
<param name= "Stringarray" type= "string" >
<para>
Original string
</para>
</param>
<param name= "findstring" type= "string" >
<para>
Match string
</para>
</param>
<returns>
Number of matching strings
</returns>
public static int Getstringcount (string sourcestring,string findstring)
{
int count = 0;
int findstringlength = Findstring.length;
string subString = sourcestring;

while (Substring.indexof (findstring) >=0)
{
subString = substring.substring (Substring.indexof (findstring) +findstringlength);
Count + 1;
}
return count;
}

<summary>
Intercepts all characters starting from startstring to the end of the original string
</summary>
<param name= "sourcestring" type= "string" >
<para>
///
</para>
</param>
<param name= "startstring" type= "string" >
<para>
///
</para>
</param>
<returns>
A String Value ...
</returns>
public static string Getsubstring (String sourcestring,string startstring)
{
Try
{
int index = Sourcestring.toupper (). IndexOf (startstring);
if (index>0)
{
Return sourcestring.substring (index);
}
return sourcestring;
}
Catch
{
Return "";
}
}

public static string Getsubstring (String sourcestring,string beginremovedstring,string endremovedstring)
{
Try
{
if (Sourcestring.indexof (beginremovedstring)!=0)
beginremovedstring = "";

if (Sourcestring.lastindexof (endremovedstring,sourcestring.length-endremovedstring.length) <0)
endremovedstring = "";

int startIndex = Beginremovedstring.length;
int length = Sourcestring.length-beginremovedstring.length-endremovedstring.length;
if (length>0)
{
Return sourcestring.substring (startindex,length);
}
return sourcestring;
}
Catch
{
return sourcestring;;
}
}

<summary>
To remove the length of a string by number of bytes
</summary>
<param name= "Strtmp" > string to be evaluated </param>
<returns> number of bytes in the string </returns>
public static int GetByteCount (string strtmp)
{
int intcharcount=0;
for (int i=0;i<strtmp.length;i++)
{
if (System.Text.UTF8Encoding.UTF8.GetByteCount (strtmp.substring (i,1)) ==3)
{
intcharcount=intcharcount+2;
}
Else
{
intcharcount=intcharcount+1;
}
}
return intcharcount;
}

<summary>
By number of bytes at the position of the string
</summary>
<param name= "Intins" > string position </param>
<param name= "Strtmp" > string to be evaluated </param>
<returns> byte position </returns>
public static int Getbyteindex (int intins,string strtmp)
{
int intreins=0;
if (Strtmp.trim () = "")
{
return intins;
}
for (int i=0;i<strtmp.length;i++)
{
if (System.Text.UTF8Encoding.UTF8.GetByteCount (strtmp.substring (i,1)) ==3)
{
intreins=intreins+2;
}
Else
{
intreins=intreins+1;
}
if (intreins>=intins)
{
intreins=i+1;
Break
}
}
return intreins;
}

}

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.