Solved

How do I set a string condition in C#?

Posted on 2009-05-14
19
316 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

861 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