Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
I have a string and a dictionary. Basically I want to iterate through all the items in the dictionary, if it finds a match in the string, it replaces that match with the dictionaryitem's name with its value in the string. I have this code:
VB
Dim writArg As String = Arguments(0)
                For Each DictItem In DeclaredVariables
                    MsgBox(DictItem.Key)
                    writArg = writArg.Replace(DictItem.Key, DictItem.Value)
                Next
                Return writArg

Where, declaredvariables hosts the variable names and values. But it won't work. It will not replace it. Can someone help?
Thanks,
-iProgramIt

EDIT:
This is the code for adding in the dictionary key. It doesn't seem to save.
VB
DeclaredVariables.Add(Arguments(0), Arguments(1))
Posted
Updated 2-Oct-15 3:02am
v2

Replace is case sensitive - so check that your argument string contains exactly the same text as the string you are comparing "Hello World!" will not match "world", or "World ".

I'd start with the debugger and look at exactly what you are trying to replace each time, and exactly what you are trying to replace it in.

It might be worth adding a check for writArg.Contains(DictItem.Key) and breaking if it finds a match. If it doesn't break...it doesn't match!
 
Share this answer
 
Comments
iProgramIt 2-Oct-15 8:59am    
Tried that. It said the dictionary contained nothing after adding a key.. that the length/count was 0.
iProgramIt 2-Oct-15 8:59am    
The length/count = 0. Made an edit ^
Maciej Los 2-Oct-15 11:08am    
Using Contains method is quite good idea. +4!
But i suggest alternative solution, please see.
OriginalGriff 2-Oct-15 11:18am    
Except two things:
1) I don't like relying on try...catch for "normal" processing - I would prefer to use Dictionary.Contains instead.
2) Your code doesn't do what his does - if the input string is "hello there what is happening?" his code would produce "hello there 123t is happening?" from your inputs. Yours would print an error message.

I suspect this is a "parameter substitution" thing, like replacing [NAME] with "Maciej Los" in a form letter.
Maciej Los 2-Oct-15 15:44pm    
It's not a matter of "liking". I really do understand that you prefer to use Contains instead of Try... End Try. I'd say: No matter of method, it's a matter of result ;)
As per my understanding OP wants to replace the name of variable with its value. So, my code does exactly what OP wants to achieve.

Thank you for your comment, Paul.

Cheers, Maciej
The simplest way is:
VB
Dim DeclaredVariables As Dictionary(Of String, String) = New Dictionary(Of String, String)

DeclaredVariables.Add("wah","123")
DeclaredVariables.Add("wha","132")
DeclaredVariables.Add("awh","213")
DeclaredVariables.Add("ahw","231")
DeclaredVariables.Add("haw","321")
DeclaredVariables.Add("hwa","312")

Dim writArg As String = "wha"
writArg = DeclaredVariables(writArg)


But i need to warn you: this may cause KeyNotFoundException[^], so you should avoid this by using Try... Catch... End Try statement[^]:
VB
Try
    writArg = DeclaredVariables(writArg)
Catch ex As KeyNotFoundException
    Console.WriteLine("{0}", ex.Message)
End Try
 
Share this answer
 
Comments
iProgramIt 3-Oct-15 22:01pm    
No. The issue is the dictionary has NO items.
Maciej Los 4-Oct-15 2:54am    
:laugh:
You didn't mention it.
Have you read my answer. There you'll find a way to create and fullfill a dictionary object.
iProgramIt 4-Oct-15 9:48am    
Hi. I found the problem. It wasn't within the class funny enough. It was how I was declaring the Command(). I was declaring locally instead of globally :)

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