字符串例程和代码库 [英] string routines and code libraries

查看:49
本文介绍了字符串例程和代码库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C#的新手,来自Delphi。在Delphi中,我使用第三方

字符串处理库,其中包含一些非常有用的字符串

函数,特别是我对BEFORE感兴趣(返回子字符串
模式之前的
),AFTER(返回模式之后的子字符串),以及

BETWEEN(返回2种模式之间的子字符串)。

我的问题是:

1.可以告诉我如何在C#中实现这样的功能吗?

2.是否可以在C~中添加/包含函数库,如果是的话? br $> b $ b怎么样?


非常感谢你的帮助。


Zoro。

推荐答案

" zoro" < IL **** @ gmail.com>写道:
"zoro" <il****@gmail.com> wrote:
我对BEFORE(在模式前返回子串
),AFTER(在模式后返回子串
)和BETWEEN(返回)感兴趣子串
2种模式之间)。 [...]
1.可以告诉我如何在C#中实现这样的功能吗?


使用String.IndexOf查找模式,String.Substring使用
提取它们之间的文本。另一种选择是定期的

表达式(在System.Text中可用),但它们的效率较低,并且有些人发现它们很难学习和理解。

2.是否可以在C~中添加/包含函数库,如果是这样的话?
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.Substring 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?




当然有可能。


在单独的类中编写函数(方法),并使用类的名称(或类的对象,前缀调用

)如果方法

是非静态的)和一个点。如果该类位于不同的命名空间中,则需要使用using命令将其导入。声明,就像你写的时候一样

使用System.IO。


P.



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.


正则表达式和常规String.SubString方法就足够了/>
regular expressions and regular String.SubString method willl be sufficient


嗨Zoro,


我不熟悉Dephi,所以我可能误解了
的含义
在这里运作。假设当你提到模式时你正在谈论关于正则表达式模式的
,我可以看到这些函数如何确实是有用的。由于我将来可能还需要这样的功能,我已经自由地写了一些.Net方法来做你所做的事情。

说话关于


///< summary>

///返回字符串中所有正则表达式匹配指数

///< / summary>

///< param name =" input">要评估的字符串< / param>

///< param name =" pattern">模式匹配< / param>

///< returns>所有匹配的索引数组< / returns>

///< remarks>如果未找到匹配项,则返回零长度数组

返回< / remarks>

public static int [] IndicesOf (字符串输入,字符串模式)

{

int [] returnVal;

正则表达式rx =新正则表达式(模式);

MatchCollection匹配= rx.Matches(输入);

returnVal = new int [matches.Count];

for(int i = 0; i< matches.Count; i ++)

返回nVal [i] =匹配[i] .Index;

返回returnVal;

}


///< summary> ;

///返回字符串中正则表达式匹配的第一个索引

///< / summary>

///< ; param name =" input">要评估的字符串< / param>

///< param name =" pattern">要匹配的模式< / param>

///< returns>输入字符串中的第一个匹配项的索引< / returns>

///< remarks>如果未找到匹配项,则返回-1< / remarks>

public static int IndexOf(字符串输入,字符串模式)

{

正则表达式rx =新正则表达式(模式);

if(!rx.IsMatch(input))返回-1;

返回rx.Match(输入).Index;

}


///< summary>

///返回输入字符串中模式的最后一个匹配的索引

///< / summary>

///< param name =" input">要评估的字符串< / param>

///< param name =" pattern">模式匹配< / param>

///< returns>输入中模式的最后一个匹配的索引

string< / returns>

public static int LastIndexOf(字符串输入,字符串模式)

{

int [ ] vals = IndicesOf(输入,模式);

if(vals.Length == 0)返回-1;

返回vals [vals.Length - 1]; < br $>
}


///< summary>

///返回字符串的子串

///在字符串中第一次出现模式之前或之后

///< / summary>

///< param name =" ;输入>字符串以评估< / param>

///< param name =" pattern">模式匹配< / param>

/ //< param name =" before">在模式之前获取子串?< / param>

///< returns>输入字符串的子串,从头开始

///的st在比赛的第一个角色之前响铃并结束,

///或者,如果之前为假,则从比赛结束开始,结束

///在字符串的末尾。< / returns>

///< remarks>如果没有匹配,则返回输入字符串。

///如果before为false,返回匹配后的子字符串< / remarks>

public static string Substring(string input,string pattern,bool before)

{

int i;

if(before)

{

i = IndexOf(输入,模式);

if(i> -1)return input.Substring(0,i);

}

else

{

Regex rx =新的正则表达式(模式);

MatchCollection匹配= rx.Matches(输入);

if(matches.Count> 0)

{

i =匹配[matches.Count - 1] .Index +匹配[matches.Count -

1] .Value.Length;

return input.Substring(i);

}

}

返回输入;

}


///< summary>

///在2个模式匹配之间查找输入字符串的子字符串

///< / summary>

///< param name =" input">要评估的字符串< / param>

///< param name =" pattern1">第一个模式< / param>

///< param name =" pattern2">第二个模式< / param>

///< ;返回> 2个模式之间的输入字符串的子串< / returns>

///< remarks>< para>模式的顺序仅为imp如果找到

/// paterns并且不是相同的模式,则重要。

///如果模式不同,并且找到了两种模式,
///返回的子字符串将是它们之间的子字符串

///,无论它们在输入文本中出现的顺序如何< / para>

///< para>如果找到了两种模式,但它们的匹配重叠,则它们之间没有任何内容,并且返回一个空白字符串< / para>

///< para>如果找到两个模式,并且它们是相同的模式,

///该方法将查找模式的第二次出现,并且

///尝试返回第一次和第二次匹配之间的子串

///使用的模式。如果没有第二场比赛,模式

///重叠,因为它们占据相同的空间,

///并且之间没有任何内容。< / para>

///< para>如果找到第一个模式,但找不到第二个模式

///,则子字符串将是子字符串

///在pattern1的第一个匹配之后的输入字符串,

///或者如果第一个模式匹配字符串的结尾,

///在pattern1< / para>第一次匹配后字符串的子字符串

///< para>如果找到第二个模式,但第一个模式不是,

///在第二个模式的第一个匹配开始之前,子字符串将是输入字符串的子字符串

///,

///或者如果第二个模式是字符串的开头,
$ b $的第一个匹配结束后字符串的

///子字符串b ///第二种模式。< / para>

/// < para>如果找不到任何模式,整个输入字符串将返回
///。< / para>

///< / remarks>

public static string SubstringBetween(string input,

string pattern1,string pattern2)

{

// indices 2匹配2种模式

int index1 = -2,index2 = -2;


// 2计算中使用的匹配

匹配m1 = null,m2 = null;

int len1,len2;


//计算第一场比赛

if(!Regex.IsMatch(input,pattern1))index1 = -1;

else

{

m1 = Regex.Match(输入,pattern1);

index1 = m1.Index;

}


//计算第二场比赛

if(!Regex.IsMatch(input,pattern2))index2 = -1;

else

{

m2 = Regex.Match (输入,模式2);

index2 = m2.Index;

}


//如果两者都找不到,返回输入

if(index1 == -1&& index2 == -1)返回输入;


//否则,至少找到1。返回一个子串


//找不到pattern1。

if(index1 == -1)

{

if(index2> 0)

返回input.Substring(0,index2); //视为第二个

else

返回input.Substring(index2 + m2.Length); //对待第一个

}


//用于没有pattern2,相同的模式和重叠


//输入到m1结尾的长度

len1 = index1 + m1.Length;


//找不到pattern2。

if(index2 == -1)

{

if(len1< input.Length)

返回input.Substring(len1) ; //对待第一个

else

返回input.Substring(0,index1); //视为第二个

}


//输入到m2结尾的长度

len2 = index2 + m2.Length ;


//测试相同的模式

if(pattern1 == pattern2)

{

int [] indices = IndicesOf(input,pattern1);

//重叠,因为两者都相同

if(indices.Length == 1)return" " ;;

返回input.Substring(len1,indices [1] - len1);

}


//不相同的模式。测试重叠


//测试重叠(index2落在m1内)

if(index2> = index1&& index2< = len1 )返回"";


//重叠测试(index1在m2内)


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


//无重叠。看看哪一个是第一个,并获得价值


// m1是第一个匹配

if(index2< index1)

返回input.Substring(len2,index1 - len2);


// m2是第一次匹配

//输入到m1的结尾的长度

len1 = index1 + m1.Length;

返回input.Substring(len1,index2 - len1);

}

///< summary>

///在字符串中第一次出现模式之前,返回字符串的子字符串

///

///< / summary>

///< param name =" input">要评估的字符串< / param>

///< param name =" pattern">模式匹配< / param>

///< returns>输入字符串的子串,从头开始

///字符串并在

模式的第一个字符前结束< / returns>

///< remarks>如果有不匹配,返回输入字符串。

public static string子串(字符串输入,字符串模式)

{

返回子串(输入,模式,真);

}


一些注意事项:您需要参考

System.Text.RegularExpressions NameSpace来使用它们。您可能希望

更改方法的名称以便清楚。我让他们在一个班级中为

做正则表达式功能,所以班级名称足以满足我的

需求。另外,请特别仔细检查Substring方法。规则

对于它来说相当复杂,并且可能不符合Delphi中的相同规则。

为了清晰起见,我对它进行了相当多的评论。关于2种模式的顺序,主要不关心

,除非找不到其中一种模式。如果没有找到它们,
将返回整个字符串。如果找不到一个模式

,它首先尝试使用它们出现的顺序,但是如果第一个模式匹配,则规则会改变,例如
,但最后

字符串,或第二个模式匹配,但在

字符串的开头。从本质上讲,它将不匹配视为空白字符串。


当然,这些方法可以扩展相当多。一些

他们只寻找一场比赛。但他们应该给你(或任何人)一个

你自己的班级图书馆的良好起点。


-

HTH ,
Kevin Spencer

Microsoft MVP

..Net开发人员

模糊度有一定的品质对它来说。


" zoro" < IL **** @ gmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...
Hi Zoro,

I''m not familiar with Dep 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">string to evaluate</param>
/// <param name="pattern">pattern to match</param>
/// <returns>Array of indices of all matches</returns>
/// <remarks>If no matches are found, zero-length array is
returned</remarks>
public static int[] IndicesOf(string input, string pattern)
{
int[] returnVal;
Regex rx = new Regex(pattern);
MatchCollection matches = rx.Matches(input);
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">string to evaluate</param>
/// <param name="pattern">pattern to match</param>
/// <returns>Index of first match in input string</returns>
/// <remarks>Returns -1 if no match is found</remarks>
public static int IndexOf(string input, string pattern)
{
Regex rx = new Regex(pattern);
if (!rx.IsMatch(input)) 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">string to evaluate</param>
/// <param name="pattern">pattern to match</param>
/// <returns>Index of the last match of a pattern in the input
string</returns>
public static int LastIndexOf(string 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">string to evaluate</param>
/// <param name="pattern">pattern to match</param>
/// <param name="before">Get the Substring before the pattern?</param>
/// <returns>Substring 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(string 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(input);
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">string to evaluate</param>
/// <param name="pattern1">first pattern</param>
/// <param name="pattern2">second pattern</param>
/// <returns>Substring 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 SubstringBetween(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(input, pattern1);
index1 = m1.Index;
}

// Calculate second match
if (!Regex.IsMatch(input, pattern2)) index2 = -1;
else
{
m2 = Regex.Match(input, 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">string to evaluate</param>
/// <param name="pattern">pattern to match</param>
/// <returns>Substring 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(string input, string pattern)
{
return Substring(input, pattern, true);
}

A couple of notes: You will need to reference the
System.Text.RegularExpressions 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.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

我是来自Delphi的C#的新手。在Delphi中,我使用的是第三方
字符串处理库,其中包含一些非常有用的字符串
函数,特别是我对BEFORE感兴趣(在模式之前返回子字符串
),AFTER (在模式之后返回子字符串),并且
BETWEEN(返回2个模式之间的子字符串)。
我的问题是:
1.能告诉我如何在C#中实现这样的功能吗?
2.是否可以在C~中添加/包含函数库,如果是的话
如何?

非常感谢您的帮助。
Zoro。



这篇关于字符串例程和代码库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆