Question how to get all the occur character position and total occurance of chracter?

kinki_2046

Member
Joined
Jun 8, 2011
Messages
23
Programming Experience
Beginner
I have a string:

abc=abc=abc=abc

So,
1. how do i get the total occurance of "="?
2. how do I get the all the occurance position for "="?
3. how do I substring the string above from the second "=" until third "="?


Thanks.
 
If the end result here is to be getting all the substrings between delimiters then you can simply call Split on your String. That will return a String array containing all those substrings.

If you really need answers to the questions you posted then you would a Do or While loop. Call IndexOf in the loop, using a value 1 greater than the previous index as the starting point. When IndexOf returns -1, there are no more instances.

As you get each index, you can add it to a collection and increment a count. From each pair of indexes, you can calculate the length of the substring in between, is required by String.Substring.
 
re

I was remember that it has a command that is "At", but I can't find it.
Do you know it?

Can you show me the both of the example?
I am just a beginner only.


thanks.
 
I was remember that it has a command that is "At", but I can't find it.
Do you know it?
There is no such command.

You don't just start to write code straight from the idea. Here's how you should approach every problem. First, forget that it's a programming problem and just think about how you would solve the problem manually, say with a pen and paper. Start by writing down the steps you would need to carry out to perform the task. Do that iteratively, refining the steps that represent multiple actions and breaking them into multiple steps. Eventually, you should have a list of elementary steps that you could simply hand to anyone and they could follow them to get the job done.

At that point, you then remember that it's actually a programming problem. You start writing pseudo-code that represents the steps you've got. Because each step is elementary, writing equivalent pseudo-code for each step is very simple. Once you've got the pseudo-code written, then you can start writing actual VB code. At that point the code should almost write itself, because you know exactly what each line has to do. That's a far easier proposition than going from a few high-level ideas straight to code.

Remember: the code is not the solution, but rather an implementation of the solution. The algorithm is the solution. Solve the problem first, i.e. develop the algorithm, before you even think about writing code.
 
Back
Top