Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

If you mean removing every digit, here is a more efficient way.

 

        'Declare string builder with the maximum capacity possibly needed to prevent memory reallocation.
       Dim tempStr As New System.Text.StringBuilder(myString.Length)

       For i As Integer = 0 To myString.Length - 1
           If Not Char.IsDigit(myString.Chars(i)) Then
               tempStr.Append(myString.Chars(i))
           End If
       Next

       Dim str As String = tempStr.ToString()

C#:

// Declare string builder with the maximum capacity possibly needed to prevent memory reallocation.
System.Text.StringBuilder tempStr = new System.Text.StringBuilder(myString.Length);

for (int i = 0; i < myString.Length; i++)
{
if (!char.IsDigit(myString[i]))
{
	tempStr.Append(myString[i]);
}
}

string str = tempStr.ToString();

This method avoids the continual memory reallocation that would result from multiple calls to String.Remove, which would be particularly important if the original string is long.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...