views:

44

answers:

3

Hi All,

I have some APSX code that I am trying to modify for a programmer that is out on medicaly leave. I am not an ASP guy, but rather C++

So what I want to do is delare a string, check the first 4 characters and if it is 'http' do something, if not, something else.

Here is what I have:

string strYT= Left(objFile, 4);

if (strYT=="http") {
    pnlYT.Visible = true;
    pnlIntro.Visible = false;
    pnlVideo.Visible = false;
}
else {
    pnlYT.Visible = false;
    pnlIntro.Visible = false;
    pnlVideo.Visible = true;

PrintText(objFile);
}

But I get errors like:

Compiler Error Message: CS0103: The name 'Left' does not exist in the class or namespace 'ASP.zen_aspx'

My googling turns up many examples of doing it just like this.....

A: 
if (myString.StartsWith("http"))
   // do stuff
else
   // do other stuff
MrSoundless
A: 
string myString = getStringFromSomeWhere();
if(myString.StartsWith("http"))
{
    doSomething();
}
else
{
    doSomethingElse();
}
Seattle Leonard
this will also match secure ` https ` pages as well.
rockinthesixstring
You're right. However, the original question did not call for handling `https` differently. He only wanted to check the first 4 characters. My guess is that if his particular string was a link, then he should do something otherwise do something else. If that is the case then just `http` is just fine.Good point though
Seattle Leonard
+2  A: 

Here is is in VB

Dim str as String = "http://mywebsite.com"

If str.StartsWith("http://") Then
    ''# This is the true stuff
    pnlYT.Visible = True
    pnlIntro.Visible = False
    pnlVideo.Visible = False
Else
    ''# This is the false stuff
    pnlYT.Visible = False
    pnlIntro.Visible = False
    pnlVideo.Visible = True
End If

Here it is in C#

string str = "http://mywebsite.com";

if (str.StartsWith("http://")) {
    // This is the true stuff
    pnlYT.Visible = true;
    pnlIntro.Visible = false;
    pnlVideo.Visible = false;

} else {
    // This is the false stuff
    pnlYT.Visible = false;
    pnlIntro.Visible = false;
    pnlVideo.Visible = true;

}
rockinthesixstring
In the other answers you'll notice that they all used `StartsWith("http")`. If you just use `StartsWith("http")` Then it will ALSO match `"https"`. If you want to separate out the secure SSL pages, then you need `StartsWith("http://")`
rockinthesixstring