473,746 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string routines and code libraries

Hi,
I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party
string handling library that includes some very useful string
functions, in particular I'm interested in BEFORE (return substring
before a pattern), AFTER (return substring after a pattern), and
BETWEEN (return substring between 2 patterns).
My questions are:
1. Can any tell me how I can implement such functionality in C#?
2. Is it possible to add/include function libraries to C~, and if so
how?

Thank you very much for your help.

Zoro.

Nov 17 '05 #1
29 4318
"zoro" <il****@gmail.c om> wrote:
I'm interested in BEFORE (return substring
before a pattern), AFTER (return substring
after a pattern), and BETWEEN (return substring
between 2 patterns). [...]
1. Can any tell me how I can implement such
functionality in C#?
Use String.IndexOf to find the patterns, and String.Substrin g to
extract the text between them. An alternative would be regular
expressions (available in System.Text), but they're less efficient and
some people find them hard to learn and understand.
2. Is it possible to add/include function libraries
to C~, and if so how?


Certainly possible.

Write your functions ("methods") in a separate class, and prefix calls
with the name of the class (or an object of the class, if the method
is non-static) and a dot. If the class is in a different namespace,
you need to import it with the "using" statement, just as you would
when you write e.g. "using System.IO".

P.
Nov 17 '05 #2
regular expressions and regular String.SubStrin g method willl be sufficient
Nov 17 '05 #3
Hi Zoro,

I'm not familiar with Dephi, so I may be misinterpreting the meaning of the
functions here. Assuming that when you mention "pattern" you are talking
about a Regular Expression-type pattern, I can see how such functions might
indeed be useful. As I may need such functions in the future as well, I've
taken the liberty of writing a few .Net methods for doing what you're
talking about:

/// <summary>
/// Returns all Indices of Regex Matches in a string
/// </summary>
/// <param name="input">st ring to evaluate</param>
/// <param name="pattern"> pattern to match</param>
/// <returns>Arra y of indices of all matches</returns>
/// <remarks>If no matches are found, zero-length array is
returned</remarks>
public static int[] IndicesOf(strin g input, string pattern)
{
int[] returnVal;
Regex rx = new Regex(pattern);
MatchCollection matches = rx.Matches(inpu t);
returnVal = new int[matches.Count];
for (int i = 0; i < matches.Count; i++)
returnVal[i] = matches[i].Index;
return returnVal;
}

/// <summary>
/// Returns first index of a Regex match in a string
/// </summary>
/// <param name="input">st ring to evaluate</param>
/// <param name="pattern"> pattern to match</param>
/// <returns>Inde x of first match in input string</returns>
/// <remarks>Return s -1 if no match is found</remarks>
public static int IndexOf(string input, string pattern)
{
Regex rx = new Regex(pattern);
if (!rx.IsMatch(in put)) return -1;
return rx.Match(input) .Index;
}

/// <summary>
/// Returns the index of the last match of a pattern in an input string
/// </summary>
/// <param name="input">st ring to evaluate</param>
/// <param name="pattern"> pattern to match</param>
/// <returns>Inde x of the last match of a pattern in the input
string</returns>
public static int LastIndexOf(str ing input, string pattern)
{
int[] vals = IndicesOf(input , pattern);
if (vals.Length == 0) return -1;
return vals[vals.Length - 1];
}

/// <summary>
/// Returns a Substring of a string
/// before or after the first occurrence of a pattern in the string
/// </summary>
/// <param name="input">st ring to evaluate</param>
/// <param name="pattern"> pattern to match</param>
/// <param name="before">G et the Substring before the pattern?</param>
/// <returns>Substr ing of input string, starting from the beginning
/// of the string and ending before the first character of the match,
/// or, if before is false, starting from the end of the match, and ending
/// at the end of the string.</returns>
/// <remarks>If there is no match, returns the input string.
/// If before is false, returns the substring after the match</remarks>
public static string Substring(strin g input, string pattern, bool before)
{
int i;
if (before)
{
i = IndexOf(input, pattern);
if (i > -1) return input.Substring (0, i);
}
else
{
Regex rx = new Regex(pattern);
MatchCollection matches = rx.Matches(inpu t);
if (matches.Count > 0)
{
i = matches[matches.Count - 1].Index + matches[matches.Count -
1].Value.Length;
return input.Substring (i);
}
}
return input;
}

/// <summary>
/// Finds a substring of ain input string between 2 pattern matches
/// </summary>
/// <param name="input">st ring to evaluate</param>
/// <param name="pattern1" >first pattern</param>
/// <param name="pattern2" >second pattern</param>
/// <returns>Substr ing of input string between the 2 patterns</returns>
/// <remarks><para> The order of the patterns is only important if both
/// paterns are found, and are not identical patterns.
/// If the patterns are different, and both patterns are found,
/// the substring returned will be the substring between them
/// regardless of the order in which they appear in the input text</para>
/// <para>If both patterns are found, but their matches overlap, there
/// is nothing between them, and a blank string is returned</para>
/// <para>If both patterns are found, and they are the same pattern,
/// The method will look for a second occurrence of the pattern, and
/// attempt to return the substring between the first and second match
/// of the pattern used. If there is not a second match, the patterns
/// overlap, as they occupy the same space,
/// and there is nothing between.</para>
/// <para>If the first pattern is found, but the second pattern
/// is not found, the substring will be either the substring
/// of the input string after the first match of pattern1,
/// or if the first pattern matches the end of the string,
/// the substring of the string after the first match of pattern1</para>
/// <para>If the second pattern is found, but the first pattern is not,
/// the substring will be either the substring of the input string
/// before the beginning of the first match of the second pattern,
/// or if the second pattern is the beginning of the string, the
/// substring of the string after the end of the first match of the
/// second pattern.</para>
/// <para>If neither pattern is found, the entire input string will
/// be returned.</para>
/// </remarks>
public static string SubstringBetwee n(string input,
string pattern1, string pattern2)
{
// indices of 2 matches matching 2 patterns
int index1 = -2, index2 = -2;

// 2 Matches to use in calculation
Match m1 = null, m2 = null;
int len1, len2;

// Calculate first match
if (!Regex.IsMatch (input, pattern1)) index1 = -1;
else
{
m1 = Regex.Match(inp ut, pattern1);
index1 = m1.Index;
}

// Calculate second match
if (!Regex.IsMatch (input, pattern2)) index2 = -1;
else
{
m2 = Regex.Match(inp ut, pattern2);
index2 = m2.Index;
}

// if neither is found, return input
if (index1 == -1 && index2 == -1) return input;

// Otherwise, at least 1 is found. Return a substring

// pattern1 not found.
if (index1 == -1)
{
if (index2 > 0)
return input.Substring (0, index2); // treat as second
else
return input.Substring (index2 + m2.Length); // treat as first
}

// Used for no pattern2, identical patterns, and overlaps

// Length of input to end of m1
len1 = index1 + m1.Length;

//pattern2 not found.
if (index2 == -1)
{
if (len1 < input.Length)
return input.Substring (len1); // treat as first
else
return input.Substring (0, index1); // treat as second
}

// Length of input to end of m2
len2 = index2 + m2.Length;

//Test for identical patterns
if (pattern1 == pattern2)
{
int[] indices = IndicesOf(input , pattern1);
// overlap, as both are the same
if (indices.Length == 1) return "";
return input.Substring (len1, indices[1] - len1);
}

// Not identical patterns. Test for overlap

// Test for overlap (index2 falls inside m1)
if (index2 >= index1 && index2 <= len1) return "";

// Test for overlap (index1 falls inside m2)

if (index1 >= index2 && index1 <= len2) return "";

// No overlap. See which one is first, and get value between

// m1 is first match
if (index2 < index1)
return input.Substring (len2, index1 - len2);

// m2 is first match
// Length of input to end of m1
len1 = index1 + m1.Length;
return input.Substring (len1, index2 - len1);
}

/// <summary>
/// Returns a Substring of a string
/// before the first occurrence of a pattern in the string
/// </summary>
/// <param name="input">st ring to evaluate</param>
/// <param name="pattern"> pattern to match</param>
/// <returns>Substr ing of input string, starting from the beginning
/// of the string and ending before the first character of the
pattern</returns>
/// <remarks>If there is no match, returns the input string.
public static string Substring(strin g input, string pattern)
{
return Substring(input , pattern, true);
}

A couple of notes: You will need to reference the
System.Text.Reg ularExpressions NameSpace to use these. You may want to
change the names of the methods for clarity. I have them in a class for
doing Regular Expression functions, so the class name is sufficient for my
needs. Also, carefully examine the Substring method in particular. The rules
for it are fairly complex, and may not conform to the same rules in Delphi.
I have commented it quite a bit for clarity. It is not primarily concerned
about the order of the 2 patterns, unless one of them is not found. It
returns the entire string if neither of them is found. If only one pattern
is not found, it attempts first to use the order in which they appear, but
the rule changes if, for example, the first pattern matches, but at the end
of the string, or the second pattern matches, but at the beginning of the
string. In essence, it treats a non-match as if it were a blank string.

Of course, these methods could be expanded an extended quite a bit. Some of
them only look for a single match. But they should give you (or anyone) a
good starting point for your own class library.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"zoro" <il****@gmail.c om> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi,
I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party
string handling library that includes some very useful string
functions, in particular I'm interested in BEFORE (return substring
before a pattern), AFTER (return substring after a pattern), and
BETWEEN (return substring between 2 patterns).
My questions are:
1. Can any tell me how I can implement such functionality in C#?
2. Is it possible to add/include function libraries to C~, and if so
how?

Thank you very much for your help.

Zoro.

Nov 17 '05 #4
Thank you very much for all the suggestions. It still looks very
complex for the simple functions I wanted:

AFTER returns the substring AFTER the pattern so:
str := AFTER('@', 'b*********@mic rosoft.com');
str = 'microsoft.com'

BEFORE returns the substring BEFORE the pattern so:
str := BEFORE('@', 'b*********@mic rosoft.com');
str = 'bill.gates

BETWEEN returns the substring BETWEEN 2 patterns so:
str := BETWEEN('@', '.', 'b*********@mic rosoft.com');
str = 'microsoft'

There must be a simpler way to achieve this in C# - surely?
Also, does anyone know of a third party library that will include such
functions?

Thanks again,

ilZoro.

Nov 17 '05 #5
zoro <il****@gmail.c om> wrote:
Thank you very much for all the suggestions. It still looks very
complex for the simple functions I wanted:

AFTER returns the substring AFTER the pattern so:
str := AFTER('@', 'b*********@mic rosoft.com');
str = 'microsoft.com'

BEFORE returns the substring BEFORE the pattern so:
str := BEFORE('@', 'b*********@mic rosoft.com');
str = 'bill.gates

BETWEEN returns the substring BETWEEN 2 patterns so:
str := BETWEEN('@', '.', 'b*********@mic rosoft.com');
str = 'microsoft'

There must be a simpler way to achieve this in C# - surely?
All of those can be done with IndexOf very easily.
Also, does anyone know of a third party library that will include such
functions?


No, but there may be one around. However, it would be only a matter of
about five minutes to write you one for the above. What else would you
want?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
public sealed class StringHelper
{

public static string Before(string pattern, string strLookup)
{
int index = strLookup.Index Of(pattern);
if(index > -1)
return strLookup.SubSt ring(0, index);
else
return null;
}

public static string After(string pattern, string strLookup)
{
int index = strLookup.Index Of(pattern);
if(index > -1)
return strLookup.SubSt ring(index, strLookup.Lengt h - index);
else
return null;
}

public static string Between(string pattern1, string pattern2, string
strLookup)
{
int index1 = strLookup.Index Of(pattern1);
int index2 = strLookup.Index Of(pattern2);

if(index1 == -1 && index2 == -1) // if either is not found, return null
return null;
else if(index1 == -1)
return strLookup.SubSt ring(index1, strLookup.Lengt h - index); // if
only first pattern is found, return after.
else if(index2 == -1)
return strLookup.SubSt ring(0, index2); // if only second pattern is
found, return before.
else
return strLookup.SubSt ring(index1, (strLookup.Leng th - index1 -
index 2)); // else return between
}

}


if you want to use this class as a librar, create a new dynamic code library
project under Visual Studio, insert this class in your project, compile it,
and then use output dll in your desired projects by referencing it.
Nov 17 '05 #7
Well, you did say "pattern." I assumed you were talking about a pattern. In
any case, all you have to do is use the functions I wrote for you. Don't
know how I could have made it any easier.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"zoro" <il****@gmail.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Thank you very much for all the suggestions. It still looks very
complex for the simple functions I wanted:

AFTER returns the substring AFTER the pattern so:
str := AFTER('@', 'b*********@mic rosoft.com');
str = 'microsoft.com'

BEFORE returns the substring BEFORE the pattern so:
str := BEFORE('@', 'b*********@mic rosoft.com');
str = 'bill.gates

BETWEEN returns the substring BETWEEN 2 patterns so:
str := BETWEEN('@', '.', 'b*********@mic rosoft.com');
str = 'microsoft'

There must be a simpler way to achieve this in C# - surely?
Also, does anyone know of a third party library that will include such
functions?

Thanks again,

ilZoro.

Nov 17 '05 #8
Thank you all for your help. Sorry, I didn't I didn't make myself
clear the first time.
Zoro.

Nov 17 '05 #9
Thanks crow - your solutions are exactly what I needed. But instead
of compiling this to a dll, shouldn't it be possible/desirable in C#
to add these functions to the system some how, by expanding the built
in string class?

Thanks,
Zoro.

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

354
15897
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets hooked up to interfaces. The Gtk is more than enough gui.
1
1840
by: Neil Schemenauer | last post by:
The title is perhaps a little too grandiose but it's the best I could think of. The change is really not large. Personally, I would be happy enough if only %s was changed and the built-in was not added. Please comment. Neil PEP: 349 Title: Generalised String Coercion
37
8538
by: Shri | last post by:
hi all, i am writing a code in which i have a char buffer "cwdir" which hold the current working directory by calling the function getcwd(). later i change the directory to "/" as i have to make my code Deamon. and later again i want to run some other executable available at the path holded by the "cwdir" using the system() system call. presently i concatenate program name (to be executed) to the "cwdir" and use system(chdir)to run the...
19
10921
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple empty strings in the resulting string array.
32
14881
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
13
3218
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I didn't found it yet. help please thank you Freaker85
5
5483
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h> /*------------------------------------------------------------------------------------------- | Function name : strrep
232
13311
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
0
1389
by: TechWitch | last post by:
In my current environment, we have a 64-bit instance (DB2/AIX64 8.2.4) where we have at least one function that was left in 32-bit mode (not JAVA). According to the IBM documentation it is possible to invoke these functions with a performance degradation at first, I assume from the libraries not matching at first. memory is being handled properly? Also, I've observed that some routines that are invoked by this 32-bit function are NOT...
0
8974
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9508
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9218
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8229
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6060
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4586
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.