Solved

How do I set a string condition in C#?

Posted on 2009-05-14
19
313 Views
Last Modified: 2013-12-17
I have created a C# web site using Visual Studio 2008. I have written the attached C# code to replace the string occurance "/intranet" in my variable with a URL path but unfortunately it has now come to my attention that this string occurance will be fairly common within other URL paths therefore breaking these URL paths. Is there anyway that I can write C# code that will only replace the "/intranet" string occurance if "http:/" is NOT written before it? i.e. "/intranet" will be replaced but "http://intranet" will not be replaced. Note: The "filteredAnnouncements" variable is a string variable containing many items from a sharepoint list which is filled by a caml query. The itemBody variable is a string variable that I have used to seperate these items.

Thanks.
string docUrl = "http://portal.shepwedd.co.uk/intranet";
 

foreach (SPListItem i in filteredAnnouncements)

            {

                //variable containing singular items

                string itemBody = i["Body"].ToString();
 

                //eliminate any white space

                string replacementString = "";

                foreach (char c in itemBody)

                {

                    if (!Char.IsWhiteSpace(c))

                    {

                        replacementString = replacementString + c.ToString();

                    }

                }
 

                //replace all p tags with div tags

                itemBody = itemBody.Replace("<p>", "<div>");

                itemBody = itemBody.Replace("</p>", "</div>");
 

                //specify the absolute URL to the document due to sharepoint stripping out the servername

                itemBody = itemBody.Replace("/intranet", docUrl);

}

Open in new window

0
Comment
Question by:Shepwedd
  • 9
  • 6
  • 2
  • +1
19 Comments
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24385110
you can split the url based on http:/, and if the resulting array has (0) and (1), it's a "http:/" URL, if it only has (0), it doesn't, replace the string...
0
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24385121
This seems like a good use of Regular Expressions. Rather than doing this manually like you are, you can use regex replacement strings, which can not only match the search strings, but also other criteria like anchoring to the start/end of the string, following certain character patterns and NOT following others.

There are a lot of reg-ex help sites out there and there are also reg-ex applications (free) that would let you build your expression interactively, testing with test data to prove it out before putting it in your application. I'm a believer in pointing people towards solutions, rather than doing all the work for them... I think it's better for you in the end.
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24385240
yup, regexp would work... but for something this simple... meh... besides, he wants a conditional, not an escapable string replace. ;)
0
 

Author Comment

by:Shepwedd
ID: 24385418
I'm just looking for a quick easy fix rather than having to learn how to use reg expressions etc.

MaxOvrdrv2, could you supply me with some sample code that will perform the split that you are suggesting? I want to only replace the string instances of "/intranet" in my variable but if this occurs after "http:/" I don't want to perform the replace. The reason why I have to do this is because sharepoint is stripping out it's own server path in my variable, i.e. "http://servername.somewhere.co.uk/intranet" is written as just "/intranet" in my variable so I need to replace the "/intranet" with "http://servername.somewhere.co.uk/intranet" so my page links can be found. The problem I have is that another server of ours has the path: "http://intranet.somewhere.co.uk" so obviously my current replace is breaking all references to this.
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24385541
ok, here it is:
strarr=theurlstring.split("http://");

strarr(1)=replace(strarr(1),"/intranet","/whateveryouwant");

theurlstring=strarr(0) + strarr(1);

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24385969
I attempted the attached but how do I declare the strarr variable as an array?
string docUrl = "http://edinspdev01/intranet";

string intranetPath = "http://intranet";
 

strarr = intranetPath.Split("http:/");

strarr(1) = replace(strarr(1), "/intranet", docUrl);

intranetPath = strarr(0) + strarr(1);

Open in new window

0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24386083
what did you get as an error? sorry i'm used to coding in ASP.Net, and you don't have to deal with that stuff in asp.net... i'd just go like this in asp.net:
dim docUrl = "/edinspdev01/intranet"

dim intranetPath = "http://intranet"

dim strarr
 

strarr=split(intranetPath,"http:/")

strarr(1)=replace(strarr(1),"/intranet",docURL)

intranetpath=strarr(0) & strarr(1)
 

sorry if this isn't clear but again, in ASP.Net, this works perfectly.

Open in new window

0
 
LVL 3

Expert Comment

by:channa_m
ID: 24386104
First
Replace http:// with "/"
and then replace "/intranet" with ""http://portal.shepwedd.co.uk/intranet"

See the code below


 string docUrl = "http://portal.shepwedd.co.uk/intranet";
 

        foreach (SPListItem i in filteredAnnouncements)

        {

            //variable containing singular items

            string itemBody = i["Body"].ToString();
 

            //eliminate any white space

            string replacementString = "";

            foreach (char c in itemBody)

            {

                if (!Char.IsWhiteSpace(c))

                {

                    replacementString = replacementString + c.ToString();

                }

            }
 

            //replace all p tags with div tags

            itemBody = itemBody.Replace("<p>", "<div>");

            itemBody = itemBody.Replace("</p>", "</div>");
 

            //specify the absolute URL to the document due to sharepoint stripping out the servername

            itemBody = itemBody.Replace("http://", "/");

            itemBody = itemBody.Replace("/intranet", docUrl);
 

        }

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24386230
channa_m,

That doesn't solve my problem as there are 2 URLs likely to appear in my variable:

http://intranet.shepwedd.co.uk/... (other server) stays as it is
http://portal.shepwedd.co.uk/intranet (sharepoint server) becomes /intranet

The code I presently have solves the sharepoint server url issue where sharepoint strips out its own server name, i.e. http://portal.shepwedd.co.uk, but when our other servers path appears in my variable its path is broken due to my replace. Therefore I need a replace which is only triggered when "http:/" doesn't appear before "/intranet".
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24386267
again, mine will do just that.
0
 

Author Comment

by:Shepwedd
ID: 24386292
MaxOvrDrv2,

I'm using asp.net, just c# asp.net. I get errors throughout my code: "using a variable like a method", "replace doesn't exist in the current context"....etc
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24386299
here it is in C#, as i see it... NOTE: some functions may be called differently than i`ve written here but you can look them up if you get any error and reformulate properly...
string docUrl = "/edinspdev01/intranet";

string intranetPath = "http://intranet";

 

strarr = split(intranetPath,"http:/");

if(UBOUND(strarr)>=1)

{

   strarr(1) = replace(strarr(1), "/intranet", docUrl);

   intranetPath = strarr(0) + strarr(1);

}

else

{

   do something else if there is no http:/ prensent in the string.

}

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24386353
Max,

Is strarr meant to be declared as a variable? Even when I do I don't get the option to use "Split"?
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24386382
strarr is a variable,
split is a function inherent to pretty much every native language know to man... it splits a string in 2, based on the provided var (in our case "http:/")... google this: C# Split equivalent
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24386483
here's the proper call:

string[] strTemp2 = strTemp1.Split('delimiter');

so in our case it would be:

string[] strarr = intranetPath.Split("http:/");

Open in new window

0
 
LVL 3

Accepted Solution

by:
channa_m earned 500 total points
ID: 24386555
Why dont you check if  the string begins with http://
and replace only if it does not
 if (!itemBody.Contains("http://intranet"))

            {

                itemBody = itemBody.Replace("/intranet", docUrl);

            }

Open in new window

0
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24386568
regex
0
 

Author Comment

by:Shepwedd
ID: 24386647
Max,

It doesn't seem to like the double quotes and when I use single quotes the error states that there are too many characters?
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24386883
C#.Net complete split example, regexp examples, text files using FSO, normal string variables, etc...

http://dotnetperls.com/Content/String-Split-Benchmark.aspx
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now