tags:

views:

349

answers:

3

Anyone know a better way to do this?

Dim Result1,Result2,FinalResult As String

Result1 = Left(sXMLResponse, sXMLResponse.IndexOf("<start>"))
Result2 = Mid(sXMLResponse, sXMLResponse.IndexOf("<end>"))

FinalResult =Result1 & Result2

Surely there is a built in String.Remove(StringOne, StringTwo) Method or something more graceful?

+4  A: 

You seem to be doing things in the old VB6 way. It is generally recommended to use the .NET functions in VB.NET, i.e. string.Substring instead of the Left/Right/Mid/etc. functions. Now it looks like you're simply trying to remove the text between and in a string, which can be done quite easily in one line.

Dim result = sXMLResponse.Substring(0, sXMLResponse.IndexOf("<start>")) & sXMLResponse.Substring(sXMLResponse.IndexOf("<end>") + 5)

Really it's not different to what you did, except it removes the need for temporary variables. Also note that there is a string.Remove method but would require a temporary variable for calculating the count parameter.

Of course you could equivalently use RegEx to remove the chunk of the string in one line, but it would be rather unnecessary for such a simple task, and certainly a good deal slower.

Noldorin
Ok great, just making sure there's nothing in the framework already for this. Thought I was missing an overload somewhere. Might use a combination of the solutions. Thanks
Paul
No prob. You could write your own extension method if you're using VB.NET 9.0 I suppose, but only bother if you're doing this regularly in code.
Noldorin
A: 

For a start, I think Remove() would remove one string from another which is not really what you want.

You want to be able to extract a string that starts with one string and ends with another. In which case, why not write your own? Something like:

function extractStr (str as string, startStr as string, endStr as string) as string
    dim sPos as integer
    dim ePos as integer

    sPos = instr (str,startStr);
    ePos = instr (str,endStr);

    extractStr = str
    if ePos > 0 then
        extractStr = left (extractStr, ePos + len(endStr))
    end if
    if sPos > 0 then
        extractStr = mid (extractStr, sPos)
    end if
end
paxdiablo
Correct me if I'm wrong, but I believe the OP is using VB.NET (albeit in a VB6 style). Although your code may compile and work in VB.NET (it's been a long time since I wrote any VB6), it is not the recommended way to perform the task.
Noldorin
Why do you think OP is using .NET?
paxdiablo
Oh, I meant to say that the IndexOf function only exists in .NET (as part of the System.String class). The VB6 equivalent is the InStr function, which you use in your post.
Noldorin
I am. BTW. using .NET
Paul
+2  A: 

You can try this solution :

Dim stringOne as String="ae234-asd-dddddffdfdff";
Dim stringTwo as String="-asd-";

stringOne.Replace(stringTwo, String.Empty);

But if there are more than one stringTwo in stringOne, they are also removed in this solution

Sessiz Saat
He's probably not removing a known text though.
Kris