Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this piece of text in a RichTextBox:

RichTextBox1.Text = "ABCDEFTEST12ABCDEF"

How can I get the repeated string value "ABCDEF"?

edit: To be more specific, I need a "duplicate finder". This image should illustrate the plan: Image

What I have tried:

I have no idea how to resolve this issue.
Posted
Updated 15-Apr-17 14:05pm
v2
Comments
Patrice T 15-Apr-17 15:59pm    
Refine your question, I don't understand what you try to do.
Member 11763983 15-Apr-17 16:36pm    
Is it clear now?
[no name] 15-Apr-17 17:30pm    
No it isn't. We don't know what your homework assignment is. Are you checking for duplicate characters? Are you checking for duplicate words in a sentence? What did your teacher tell you to do? What were you studying in class?
Member 11763983 15-Apr-17 17:48pm    
It's no homework and I'm not studying. I want to do this because I'm interested. I need a solution to find duplicate characters.
[no name] 15-Apr-17 18:28pm    
If you do not study, how do you expect to learn? Study how to iterate through an array. Then learn how to ask questions. And get a book on programming and work though it.

The answer lies in your question!

  How can I get the repeated string value "ABCDEF"?

I think what you really meant was "how do I detect a repeated word in a bunch of text?"

The first step would be to break up the input text into a collection of words.  Then, check for duplicate entries in the collection, perhaps by counting the occurences of each word.  Words that occur more than once are your duplicates.

As you can see, I've broken your complex problem into two somewhat less complex problems.  Your job now is to attack each of these problems and break them up into even simpler problems.  This is called stepwise refinement and is at the core of software engineering - you break up a fairly abstract and complex task into a collection of simple and very well defined (and therefore programmable) tasks.

Some things that may help you achieve your goal are the String.Split() method[^] and the C# Dictionary[^] class which you can use to count occurences of each word you've discovered.

/ravi
 
Share this answer
 
v2
Comments
Member 11763983 15-Apr-17 20:08pm    
Thanks, I got it now.

But what if there were no spaces in my textbox like in my first question:

RichTextBox1.Text = "ABCDEFTEST12ABCDEF"

What would be possible here? Do I have to check each char separately or is there a better way to find duplicate occurences?

Ravi Bhavnani 15-Apr-17 21:21pm    
> But what if there were no spaces in my textbox

You have to first establish your program's requirements. I assumed you want to identify duplicate WORDS, where a WORD is text separated by one or more spaces. Searching for duplicate segments in an unbroken string is a (much) more complex problem.

So the question I have is, what kind of data is your program supposed to consume?

/ravi
VB
Dim str() As String = {}
str = Split(RichTextBox1.Text, " ")
Dim str2 As String = ""
Dim str3() As String = {}
Dim duplicate_names As String = ""
Dim i As Integer = 1
For Each name As String In str
    str3 = Split(str2, " ")
    For Each name2 As String In str3
        If name = name2 Then
            If Not duplicate_names.Contains(name) Then
                duplicate_names += name & vbNewLine
                i += 1
            End If
        End If
    Next
    str2 += name & " "
Next
MsgBox(duplicate_names)
 
Share this answer
 
v2
Comments
Ravi Bhavnani 15-Apr-17 20:15pm    
Does your program work correctly with this text: BAND AND BAND? I would expect your program to identify "BAND" as the only duplicate word.

/ravi
Patrice T 15-Apr-17 20:45pm    
Your code look wrong. As far as I understand "and band" and "band and" do not gibe same result.
Member 11763983 16-Apr-17 5:23am    
Ya, it still don't work perfectly with "band and band".. but I let it be.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900