poor string function

  • Thread starter Thread starter ALI-R
  • Start date Start date
A

ALI-R

why c# function for string manupolation is not as strong as other languages
,for extracting a substring from a middle of another string ,I have a lot of
problems.

Any suggestion or I am in the wrong spot.
Thanks
 
hi.....

seems like a heated debate on this topic, count me in.......

if u cud say the issue, probably we can give you some solutions or may be
suggestions.....

Kannan.V
 
ALI-R said:
why c# function for string manupolation is not as strong as other languages

C# itself has almost *no* string manipulation at all. The .NET
framework has plenty though. That's the way it should be, IMO.
for extracting a substring from a middle of another string ,I have a lot of
problems.

That sounds like your problem rather than the framework's or C#'s, but
perhaps if you could tell us more about the problems you're having, we
could help you. Here's an example of getting a substring:

string x = "abcdefg";
string y = x.Substring (2, 3);

y is now "cde" - 3 characters, starting from position 2 ('c') of x.
 
Because they're slower, and unnecessary if you already know the position in
the string you want to extract from. But if you don't, then you should use
them.
 
Because they're slower, and unnecessary if you already know the position in
the string you want to extract from. But if you don't, then you should use
them.

Again, not necessarily. If you want to get the rest of the string from
the first colon onwards, using a regular expression seems over the top
to me when IndexOf and Substring will do the trick perfectly easily.
 
Seems to be stimulating your sense of proud self-confidence on C#
capability in string manipulation,ok here we go,GET FIGHTED:


I have a string like string pathFolder=
"http://serverdev.hag.net/Operation/Document Library"

I want to split it into these three segments:
1) "http://serverdev.hag.net"
2)"Operation"
3)Document Library

I did it in this way but I don't like the way that I didit,is there a better
of doing that?

docLibName= pathFolder.Substring(pathFolder.LastIndexOf("/") + 1);

subarea= tmpStr.Substring(tmpStr.LastIndexOf("/") + 1);

portalName=
tmpStr.Remove(tmpStr.LastIndexOf("/"),(tmpStr.Substring(tmpStr.LastIndexOf("
/"))).Length);



thanks for your help
 
Sorry here is the complete code I used"

docLibName= pathFolder.Substring(pathFolder.LastIndexOf("/") + 1);


string tmpStr= pathFolder.Remove(pathFolder.Length -
docLibName.Length -1,docLibName.Length + 1);

subarea= tmpStr.Substring(tmpStr.LastIndexOf("/") + 1);

portalName=
tmpStr.Remove(tmpStr.LastIndexOf("/"),(tmpStr.Substring(tmpStr.LastIndexOf("
/"))).Length);
 
ALI-R said:
Sorry here is the complete code I used"

docLibName= pathFolder.Substring(pathFolder.LastIndexOf("/") + 1);
string tmpStr= pathFolder.Remove(pathFolder.Length -
docLibName.Length -1,docLibName.Length + 1);

I think I'd just use

int lastSlash = pathFolder.LastIndexOf('/');

// Do some error checking here, just in case there aren't any slashes
// or it's reached the http:// part

string docLibName = pathFolder.Substring (lastSlash+1);
string previousSlash = pathFolder.LastIndexOf('/', lastSlash-1);

// Again, do error checking

string subArea = pathFolder.Substring (previousSlash+1,
lastSlash-previousSlash-1);
string portalName = pathFolder.Substring (0, previousSlash);

Alternatively, take the substring after http:// and then use
String.Split.
 
ALI-R,
Have you tried using System.Uri and/or System.IO.Path to parse the file name
into the respective parts?

Something like:

string pathFolder = "http://serverdev.hag.net/Operation/Document
Library";
Uri uri = new Uri(pathFolder);
// 1) "http://serverdev.hag.net"
string portalName = uri.Host;

// 2)"Operation"
string subarea = Path.GetDirectoryName(uri.LocalPath);

// 3)Document Library
string docLibName= Path.GetFileName(uri.LocalPath);

Hope this helps
Jay
 
Back
Top