如何在字符串中查找制表符和空格 [英] how to find a tabs and spaces in a string

查看:102
本文介绍了如何在字符串中查找制表符和空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串
str ="123 456";

在3和4之间有空格和制表符,我该如何确定
制表符中的空格

 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用使用System.Drawing;
使用 System.Linq;
使用 System.Text;
使用使用System.Windows.Forms;

命名空间问题
{
    公共 部分  class  Form1:表单
    {
        公共 Form1()
        {
            InitializeComponent();
            pp();
        }

        私有 无效 pp()
        {
             int  i,SumTab,SumSpace,len;
            字符串 str;
            字符 [] characterArray =  字符 [  250 ];

            str = " ;
            SumTab =  0 ;
            SumSpace =  0 ;
            len = str.Length;
            str.CopyTo( 0 ,characterArray, 0 ,len);
             for (i =  0 ; i ><  str.Length; i ++)
            {
                如果(characterArray [i] == ' ')// 空格
                    SumSpace ++;
                其他 如果(characterArray [i] == ' span> ')//  ????? 
                    SumTab ++;
                其他;
            }
        }
   }
} 

解决方案

\t是制表符.

 如果(characterArray [i] == ' 其他 如果(characterArray [i] == '  \ t')
  SumTab ++; 


  if (characterArray [i] == '  \ t')
   SumTab ++; 



另外,您可能要包括对"\ n"(新行#10),"\ r"(回车符#13),"\ b"(退格键#8)和Unicode的检查数字形式的字符等.

请参阅: http://msdn.microsoft.com/en-us/library /aa691087(v=vs.71).aspx [ ^ ].

—SA


您还可以使用linq:

 公共  int  CharCount(字符字符串 myString)
{
     int  count =(来自,c  in  myString
                 其中 c ==字符
                 选择 c).Count();
    返回计数;
} 



然后这样称呼:

  int  spaceCount = CharCount(' "   123 456");
 int  tabCount = CharCount('  \ t'" ); 


i have string
str="123 456";

between 3 and 4 there are spaces and tabs ,how can i decern
the spaces from tabs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ques
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pp();
        }

        private void pp()
        {
            int i,SumTab, SumSpace, len;
            string str;
            char[] characterArray = new char[250];

            str = "123       456";
            SumTab = 0;
            SumSpace = 0;
            len = str.Length;
            str.CopyTo(0, characterArray, 0, len);
            for (i = 0; i < str.Length; i++)
            {
                if (characterArray[i] == ' ') //Space
                    SumSpace++;
                else if (characterArray[i] == ' ')//?????
                    SumTab++;
                else ;
            }
        }
   }
}

解决方案

\t is tab.

if (characterArray[i] == ' ')
  SumSpace++;
else if (characterArray[i] == '\t')
  SumTab++;


if (characterArray[i] == '\t')
   SumTab++;



Also, you might want to include the checks for ''\n'' (new line #10), ''\r'' (carriage return #13), ''\b'' (backspace, #8), Unicode characters in numeric forms, and more.

See: http://msdn.microsoft.com/en-us/library/aa691087(v=vs.71).aspx[^].

—SA


You can also use linq:

public int CharCount(char character, string myString)
{
    int count = (from c in myString 
                 where c == character
                 select c).Count();
    return count;
}



And call it like this:

int spaceCount = CharCount(' ', "123 456");
int tabCount   = CharCount('\t', "123 456");


这篇关于如何在字符串中查找制表符和空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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