lothos12345 Posted July 5, 2005 Posted July 5, 2005 Is there a way to search for any integer value in a string and remove it? Any help given is greatly appreciated. Quote
Diesel Posted July 5, 2005 Posted July 5, 2005 string str = "abc124de6g"; for (int i = 0; i < str.Length; ++i) { if (Char.IsNumber(str, i)) str.Remove(i, 1); } Quote
jmcilhinney Posted July 6, 2005 Posted July 6, 2005 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. Quote
HJB417 Posted July 6, 2005 Posted July 6, 2005 System.Text.RegularExpressions.Regex.Replace(text, "d", ""); Quote
jmcilhinney Posted July 6, 2005 Posted July 6, 2005 System.Text.RegularExpressions.Regex.Replace(text' date=' "d", "");[/quote']Nice. Haven't used Regex too much but it just seems to get usefuller and usefuller. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.