2

I'm new in c#. and I have some Question...

I have String following this code

string taxNumber = "1222233333445";

I want to get data from This string like that

string a = "1"
string b = "2222"
string c = "33333"
string d = "44"
string e = "5"

Please Tell me about Method for get Data From String.

Thank You Very Much ^^

1
  • what you have tried??? Commented Jul 31, 2013 at 5:17

6 Answers 6

4

Use the String.Substring(int index, int length) method

string a = taxNumber.Substring(0, 1);
string b = taxNumber.Substring(1, 4);
// etc
Sign up to request clarification or add additional context in comments.

Comments

3

Oh well, the best I can come up with is this:

IEnumerable<string> numbers
    = taxNumber.ToCharArray()
               .Distinct()
               .Select(c => new string(c, taxNumber.Count(t => t == c)));

foreach (string numberGroup in numbers)
{
    Console.WriteLine(numberGroup);
}

Outputs:

1 
2222 
33333 
44 
5

Comments

2

This can also do , you dont need to fix the no of characters, you can check by changing the no of 1's , 2's etc

string taxNumber = "1222233333445";
           string s1 = taxNumber.Substring(taxNumber.IndexOf("1"), ((taxNumber.Length - taxNumber.IndexOf("1")) - (taxNumber.Length - taxNumber.LastIndexOf("1"))) + 1);
           string s2 = taxNumber.Substring(taxNumber.IndexOf("2"), ((taxNumber.Length - taxNumber.IndexOf("2")) - (taxNumber.Length - taxNumber.LastIndexOf("2"))) + 1);
           string s3 = taxNumber.Substring(taxNumber.IndexOf("3"), ((taxNumber.Length - taxNumber.IndexOf("3")) - (taxNumber.Length - taxNumber.LastIndexOf("3"))) + 1);

Comments

0

You can use Char.IsDigit to identify digits out of string, and may apply further logic as follows:

 for (int i=0; i< taxNumber.Length; i++)
    {
        if (Char.IsDigit(taxNumber[i]))
          {
                  if(taxNumber[i-1]==taxNumber[i])
                   {
                     /*Further assign values*/ 
                   }
          }

Comments

0

Try this Code

    string taxNumber = "1222233333445";           

    char[] aa = taxNumber.ToCharArray();
    List<string> finals = new List<string>();

    string temp = string.Empty;
    for (int i = 0; i < aa.Length; i++)
    {
        if (i == 0)
        {
            temp = aa[i].ToString();
        }
        else
        {
            if (aa[i].ToString() == aa[i - 1].ToString())
            {
                temp += aa[i];
            }
            else
            {
                if (temp != string.Empty)
                {
                    finals.Add(temp);
                    temp = aa[i].ToString();
                }
            }
            if (i == aa.Length - 1)
            {
                if (aa[i].ToString() != aa[i - 1].ToString())
                {
                    temp = aa[i].ToString();
                    finals.Add(temp);
                }
                else
                {
                    finals.Add(temp);
                }
            }
        }

    }

and check value of finals string list

Comments

0

you may use regex:

string strRegex = @"(1+|2+|3+|4+|5+|6+|7+|8+|9+|0+)";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"1222233333445";

return myRegex.Split(strTargetString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.