Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Striping out alpha characters 1

Status
Not open for further replies.

Chance1234

IS-IT--Management
Jul 25, 2001
7,871
US
Code:
      private void button3_Click(object sender, EventArgs e)
        {
            string strip = "84;#Tim nTest;#57;#Diana Piece;#85;#Ian Acme;#88;#Matthew MadeUp";

                for (int i = 57; i <= 127 ; i++)
			        {
        			                   strip = strip.Replace((char)i,''); //second argument here is the error
                   
			        }
            textBox3.Text = strip.ToString();


        }

What I am trying to do is strip out all the characters except the # and the numbers. But I cant seem to get teh second argument for the replace function right. If i use a char code of a value no problems, but if i try, as in above I get an error.

Chance,

F, G + 1MSTG
 
just to add a ps onto this,

Ive been going through the help files, and I can't seem to be able to find the awnsers to what I am trying to acheive as an end result. In the sense of I can see functions such as string.override, replace etc , but can't get them to fit my solution.

I have the follownig VBA code below

Code:
Sub strid()

Dim strText(4) As String
Dim intX, IntY, intZ As Integer

strText(1) = "1;#Robert Testing"
strText(2) = "6;#John Sample;#8;#Mary Chaos"
strText(3) = "84;#Tim nTest;#86;#Chris Random;#87;#Andrew Case"
strText(4) = "84;#Tim nTest;#57;#Diana Piece;#85;#Ian Acme;#88;#Matthew MadeUp"

    For I = 1 To 4
        For X = 59 To 127
           strText(I) = Replace(strText(I), Chr(X), "")
        Next
    Next
'------------- next bit
For I = 1 To 4
    
    intX = 1
    IntY = Len(strText(I))
    
    Do Until intX >= IntY
        intZ = InStr(intX, strText(I), "#")
        Debug.Print CInt(Mid(strText(I), intX, intZ - intX)) '& "               " & strText(I)
        intX = intZ + 1
        
        If Mid(strText(I), intX + 1, 1) = "#" Then
            intX = intX + 2
        End If
    Loop

Next

End Sub

What Iam trying to do is replicate that in C Sharp

So from the strings declared in teh abvoe i end up with

1
6
8
84
86
87
84
57
85
88



Chance,

F, G + 1MSTG
 
This achieves the same output as the VBA example, however your description of what you are after doesn't match the VBA output.

Code:
		private void button1_Click(object sender, EventArgs e)
		{
			string strip = "84;#Tim nTest;#57;#Diana Piece;#85;#Ian Acme;#88;#Matthew MadeUp";
			foreach(System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(strip, "\\d+"))
			{
				listBox1.Items.Add(m.ToString());
			}
		}


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top