返回一行中相对数字比较集中最后的一个索引位置

园子里有人问起这个问题:
有字符串如下:

ds32Christchurch   2 b   1200       - Rosamond J.S3.     
ston         2 a     916       - Sidney 34
5 a     408       - Thomas A. 12 Youkq k
3hitehaven  10 b   1317
要求返回一行中相对数字比较集中最后的一个索引
以上结果应为
1200 后面的第一个位置
916       后面的一个位置
408  后面的一个位置
1317   后面的一个位置

匆忙写下算法,留待改进:

基本算法:先把字符串转成一个字符数组,然后循环,取三个变量maxLength,tempMax,maxIndexPosition,,当连续为数字 时,tempLength++,然后与maxLength比较,记下maxLength和maxIndexPosition,直到行结束。

代码:

ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif        
/// 获取一行字符串中数字连续了大值的位置,如
InBlock.gif        
/// static string str = @"ds32Christchurch   2 b   1200       - Rosamond J.S3.     ";
InBlock.gif        
/// 中的位置为1200后,即29
InBlock.gif        
/// downmoon(邀月) 2009.9.18 3w@live.cn
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sOrg">原始字符串</param>
ExpandedBlockEnd.gif        
/// <returns></returns> 

None.gif        public static int GetPosition(string sOrg)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
int result = -1;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (string.IsNullOrEmpty(sOrg)) dot.gifreturn result; }
InBlock.gif            
int maxlength = 0;//最大连续长度
InBlock.gif
            int maxIndex = -1;//怪大连续值的索引位置
InBlock.gif
            int index = -1;//临时索引
InBlock.gif
            int temp = 0;//临时变量,用于和最大连续值的比较
InBlock.gif
            foreach (char c in sOrg)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                index
++;//索引位置累加
InBlock.gif
                if (Char.IsDigit(c))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    temp
++;
InBlock.gif                    
if (temp > maxlength)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        maxlength 
= temp;//记录连续数字最大值
InBlock.gif
                        maxIndex = index + 1;//记录连续数字最大值的索引位置
InBlock.gif
                        result = maxIndex;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
else dot.gif{ temp = 0; }
InBlock.gif               
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedBlockEnd.gif        }

测试代码:

None.gif    public   static   void  Main( string [] args)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string str = @"ds32Christchurch   2 b   1200       - Rosamond J.S3.     ";
InBlock.gif            
//string str = "ston         2 a     916       - Sidney 34 ";
InBlock.gif
            Console.WriteLine("该行中数字比较集中的位置是:" + GetPosition(str));
InBlock.gif            Console.ReadKey();
ExpandedBlockEnd.gif        }


得出结果:29

代码说明:

注意,此处使用Char.IsDigit(c) 没有用 Char.IsNumber(c)

关于这两个方法的区别,请查看MSDN

这个可以做个简单测试 

None.gif   public   static   void  TestNumber()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
for (int i = 0; i <= 0xffff++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
char c = (char)i;
InBlock.gif
InBlock.gif                
if (Char.IsDigit(c) != Char.IsNumber(c))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(
"Char value {0:x} IsDigit() = {1}, IsNumber() = {2}", i, Char.IsDigit(c), Char.IsNumber(c));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/downmoon/archive/2009/09/18/1569649.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值