• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

How do I set a string condition in C#?

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
Shepwedd
Asked:
Shepwedd
  • 9
  • 6
  • 2
  • +1
1 Solution
 
MaxOvrdrv2Commented:
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
 
ToddBeaulieuCommented:
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
 
MaxOvrdrv2Commented:
yup, regexp would work... but for something this simple... meh... besides, he wants a conditional, not an escapable string replace. ;)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ShepweddAuthor Commented:
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
 
MaxOvrdrv2Commented:
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
 
ShepweddAuthor Commented:
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
 
MaxOvrdrv2Commented:
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
 
channa_mCommented:
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
 
ShepweddAuthor Commented:
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
 
MaxOvrdrv2Commented:
again, mine will do just that.
0
 
ShepweddAuthor Commented:
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
 
MaxOvrdrv2Commented:
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
 
ShepweddAuthor Commented:
Max,

Is strarr meant to be declared as a variable? Even when I do I don't get the option to use "Split"?
0
 
MaxOvrdrv2Commented:
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
 
MaxOvrdrv2Commented:
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
 
channa_mCommented:
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
 
ToddBeaulieuCommented:
regex
0
 
ShepweddAuthor Commented:
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
 
MaxOvrdrv2Commented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 9
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now