vb.net - Clear rich text box and leave certain strings -
i have lots of data in rich text box, trying clear , leave words begin az, possible @ all? have tried following coding fails
string cannot of 0 length. parameter name: oldvalue
the code is
private sub button3_click(sender system.object, e system.eventargs) handles button3.click dim str string str = rtbjw.text if not str.contains("az") = true rtbjw.text = rtbjw.text.replace("", "").trim() end if end sub
example text
az0034 14 az0034l1 art3 ltd 0 srv01 000_00000_ltd ecr 0 - active - az0003 10 az0003l1 art3 ltd 0 srv01 000_00000_ltd ecr 8 - active - az0008 9 az0008l1 art3 ltd 0 srv01 000_00000_ltd ecr 12 - active - az0010 8 az0010l1 art3 ltd 0 srv01 000_00000_ltd ecr 7 - active - az0009 7 az0009l1 art3 ltd 0 srv01 000_00000_ltd ecr 7 - active - az0004 2 az0004l1 art3 ltd 0 srv01 000_00000_ltd ecr 3 - active - az0006 1 az0006l1 art3 none - - --- ecr 7 - active - az0007 - az0007l1 art3 none - - jo ecr 8 - active -
rtbjw.text = rtbjw.text.replace("", "").trim()
will replace empty strings found within rtjbw.text
empty strings. i'm sure noticed didn't anything, because there aren't empty strings replace. @ rate, if want leave words begin in "az" not right approach. few different methods, including regex work. however, i'm not familiar regex enough provide solution using it, work well.
private sub button3_click(sender object, e eventargs) handles button3.click dim strarr() string = rtbjw.text.split(" ") rtbjw.clear() each item in strarr if item.startswith("az") if rtbjw.text.length > 0 rtbjw.text &= " " rtbjw.text &= item end if next end sub
what did here take contents of rtbjw
, assign string array, splitting space. creates list of "words" (assuming each word separated space). since have list of "words" in textbox, textbox cleared of text.
then, in loop, i'm looking @ each item in list of words, , if begins "az" (using startswith()
method), i'm doing 2 things. first, if length of text in textbox greater 0, add space what's in textbox (to separate words, , greater 0 check prevent adding space first character), adding word begins "az" end of text in textbox.
edit: because curious , i'm learning how use them, modified use linq query instead. same thing, instead of assigning text string array, text stored in queryresult variable. see following.
private sub button3_click(sender object, e eventargs) handles button3.click dim queryresults ienumerable = word in rtbjw.text.split(" ") word.startswith("az") rtbjw.clear() each word in queryresults if rtbjw.textlength > 0 rtbjw.text &= " " rtbjw.text &= word next end sub
Comments
Post a Comment