更新时间:2023-02-26 12:31:39
我会做这个,我将创建一个Hash,其中字母(char)为键,
值为0。
我将迭代字符串的字符并设置值
像这样的东西:
Hashtable hash = new Hashtable();
hash [''a' '] = 0;
hash [''b''] = 0;
hash [''c''] = 0;
....
foreach(char c in string.ToCharArray())
hash [c] = 1;
foreach(int in hash.Values)
if(0 == i)
返回false;
返回true;
欢呼,
-
Ignacio Machin,
ignacio。 machin AT dot.state.fl.us
佛罗里达州交通局
" booksnore" <博******* @ netscape.net>在消息中写道
新闻:eG ************** @ TK2MSFTNGP14.phx.gbl ...我正在写一些代码搜索包含字母表中每个字母的字符串。目前我正在使用下面的方法检查
查看字符串是否包含字母表中的每个字母。我想使用
正则表达式,但我在
正则表达式中找不到''AND''运算符 - 所以如果字符串
包含''a',我需要匹配true '和''''和''''和'''''..等等。如果有人对我如何比我正在做的更有效地提供检查有任何想法,那么目前非常感谢帮助。
private static bool InAlphabet(字符串文本)
如果(text == null)
{
返回false; >
}
int count = 97;
bool IsValid = true;
while(IsValid&& count< 123)
// //从整数类型中转换字符
string regexStr ="(" +(char)count +" )" ;;
正则表达式regex_x =新正则表达式(regexStr);
匹配m_x = regex_x.Match(文本);
IsValid = m_x 。成功;
count ++;
}
返回IsValid;
}
***通过Developersdex发送 http://www.developersdex.com ***
使用位图可能比分配哈希表更有效...
const int CC_A =(int)''A''; // 65
const int CC_Z =(int)''Z''; // 90
const uint ONE = 0x01;
const uint MAX = 0xFFFFFFFF; // UInt32.MaxValue
string str =" abcdefghijklmnopqrstuvwxyz" ;;
uint map = 4227858432; //前26位OFF
int cc = 0; // char code
foreach(字符串中的字符串)
{
cc =(int)Char.ToUpper(ch );
if(cc> = CC_A&& cc< = CC_Z)
{
//在一行...
// map | =(ONE<<(cc - CC_A));
//一步一步。 ..
//从字母代码中减去65,这样掩模就可以适合32位
//
cc - = CC_A;
//通过将1位移动适当数量的位置来创建掩码
//
uint mask = ONE << cc;
//重新分配位图
//
map | = mask;
}
}
if(map == MAX)
{
// TRUE, string包含所有26个字母表字符
}
" Ignacio Machin(.NET / C#MVP)"写道:
我会这样做,我将创建一个哈希,字母(字符)为键,值为0为值。
我会迭代字符串的字符并设置值
这样的东西:
Hashtable hash = new Hashtable();
hash [''a''] = 0;
hash [''b''] = 0;
hash [''c''] = 0;
....
foreach(char c in string.ToCharArray())
hash [c] = 1;
foreach(int i in hash.Values)
if(0 == i)
返回false;
返回true;
欢呼,
-
Ignacio Machin,
ignacio.machin at dot.state.fl.us
佛罗里达州交通局
" booksnore" <博******* @ netscape.net>在消息中写道
新闻:eG ************** @ TK2MSFTNGP14.phx.gbl ...我正在编写一些代码来搜索字符串包含字母表的每个字母。目前我正在使用下面的方法检查
查看字符串是否包含字母表中的每个字母。我想使用
正则表达式,但我在
正则表达式中找不到''AND''运算符 - 所以如果字符串
包含''a',我需要匹配true '和''''和''''和'''''..等等。如果有人对我如何比我正在做的更有效地提供检查有任何想法,那么目前非常感谢帮助。
private static bool InAlphabet(字符串文本)
如果(text == null)
{
返回false; >
}
int count = 97;
bool IsValid = true;
while(IsValid&& count< 123)
// //从整数类型中转换字符
string regexStr ="(" +(char)count +" )" ;;
正则表达式regex_x =新正则表达式(regexStr);
匹配m_x = regex_x.Match(文本);
IsValid = m_x 。成功;
count ++;
}
返回IsValid;
}
***通过Developersdex发送 http://www.developersdex.com ***
KH< KH@discussions.microsoft.com>写道:使用位图可能比分配哈希表更有效...
确实 - 不容易理解代码如使用布尔数组,
但是:
静态bool ContainsAllLetters(字符串x)
{
bool [] letters = new bool [26];
int lettersFound = 0;
foreach(char c in x)
{
int index = char.ToUpper(c) - ''A'';
if(index> = 0&&
index< letters.Length&&
!letters [index])
{
letters [index] = true;
lettersFound ++;
}
}
return(lettersFound == letters.Length);
}
请注意,这不会发现有重音符号的字符 - 但是在
-
Jon Skeet - < sk *** @pobox。 com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
I am writing some code to search for strings that contain every letter
of the alphabet. At the moment I am using the method below to check to
see if a string contains every letter of the alphabet. I wanted to use a
regular expression but I could not find an ?AND? operator within the
regular expression ? so I need something like match true if string
contains ?a? and ?b? and ?c? and ?d? ..etc,etc. If anyone has any
thoughts on how I can provide the check more effectively than I am doing
at the moment the help would be very much appreciated.
private static bool InAlphabet(string text)
{
if(text==null)
{
return false;
}
int count = 97;
bool IsValid = true;
while (IsValid && count < 123)
{
// Cast character from integral type
string regexStr = "(" + (char)count + ")";
Regex regex_x = new Regex(regexStr);
Match m_x = regex_x.Match(text);
IsValid = m_x.Success;
count++;
}
return IsValid;
}
*** Sent via Developersdex http://www.developersdex.com ***
Hi,
I would do this, I will create a Hash with the letters (char) as key and
value 0 as values.
I will iterate in the chars of the string and set the value
somethins like this:
Hashtable hash = new Hashtable();
hash[''a'']=0;
hash[''b'']=0;
hash[''c'']=0;
....
foreach( char c in string.ToCharArray() )
hash[ c ] = 1;
foreach( int i in hash.Values )
if ( 0 == i )
return false;
return true;
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"booksnore" <bo*******@netscape.net> wrote in message
news:eG**************@TK2MSFTNGP14.phx.gbl...I am writing some code to search for strings that contain every letter
of the alphabet. At the moment I am using the method below to check to
see if a string contains every letter of the alphabet. I wanted to use a
regular expression but I could not find an ''AND'' operator within the
regular expression - so I need something like match true if string
contains ''a'' and ''b'' and ''c'' and ''d'' ..etc,etc. If anyone has any
thoughts on how I can provide the check more effectively than I am doing
at the moment the help would be very much appreciated.
private static bool InAlphabet(string text)
{
if(text==null)
{
return false;
}
int count = 97;
bool IsValid = true;
while (IsValid && count < 123)
{
// Cast character from integral type
string regexStr = "(" + (char)count + ")";
Regex regex_x = new Regex(regexStr);
Match m_x = regex_x.Match(text);
IsValid = m_x.Success;
count++;
}
return IsValid;
}
*** Sent via Developersdex http://www.developersdex.com ***
Using a bitmap might be more efficient than allocating a hash table ...
const int CC_A = (int)''A''; // 65
const int CC_Z = (int)''Z''; // 90
const uint ONE = 0x01;
const uint MAX = 0xFFFFFFFF; // UInt32.MaxValue
string str = "abcdefghijklmnopqrstuvwxyz";
uint map = 4227858432; // first 26 bits OFF
int cc = 0; // char code
foreach (char ch in str)
{
cc = (int)Char.ToUpper(ch);
if (cc >= CC_A && cc <= CC_Z)
{
// In one line ...
// map |= (ONE << (cc - CC_A));
// Step by step...
// Subtract 65 from the char code so the mask fits in 32 bits
//
cc -= CC_A;
// Create the mask by shifting 1 bit the appropriate number of places
//
uint mask = ONE << cc;
// Reassign the bitmap
//
map |= mask;
}
}
if (map == MAX)
{
// TRUE, string contains all 26 alphabet chars
}
"Ignacio Machin ( .NET/ C# MVP )" wrote:Hi,
I would do this, I will create a Hash with the letters (char) as key and
value 0 as values.
I will iterate in the chars of the string and set the value
somethins like this:
Hashtable hash = new Hashtable();
hash[''a'']=0;
hash[''b'']=0;
hash[''c'']=0;
....
foreach( char c in string.ToCharArray() )
hash[ c ] = 1;
foreach( int i in hash.Values )
if ( 0 == i )
return false;
return true;
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"booksnore" <bo*******@netscape.net> wrote in message
news:eG**************@TK2MSFTNGP14.phx.gbl...I am writing some code to search for strings that contain every letter
of the alphabet. At the moment I am using the method below to check to
see if a string contains every letter of the alphabet. I wanted to use a
regular expression but I could not find an ''AND'' operator within the
regular expression - so I need something like match true if string
contains ''a'' and ''b'' and ''c'' and ''d'' ..etc,etc. If anyone has any
thoughts on how I can provide the check more effectively than I am doing
at the moment the help would be very much appreciated.
private static bool InAlphabet(string text)
{
if(text==null)
{
return false;
}
int count = 97;
bool IsValid = true;
while (IsValid && count < 123)
{
// Cast character from integral type
string regexStr = "(" + (char)count + ")";
Regex regex_x = new Regex(regexStr);
Match m_x = regex_x.Match(text);
IsValid = m_x.Success;
count++;
}
return IsValid;
}
*** Sent via Developersdex http://www.developersdex.com ***
KH <KH@discussions.microsoft.com> wrote:Using a bitmap might be more efficient than allocating a hash table ...
Indeed - not as easy to understand the code as using a boolean array,
however:
static bool ContainsAllLetters(string x)
{
bool[] letters = new bool[26];
int lettersFound=0;
foreach (char c in x)
{
int index = char.ToUpper(c)-''A'';
if (index >= 0 &&
index < letters.Length &&
!letters[index])
{
letters[index]=true;
lettersFound++;
}
}
return (lettersFound==letters.Length);
}
Note that this won''t spot characters which have accents etc - but at
least it doesn''t fail if you include any numbers.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too