4 Replies - 10497 Views - Last Post: 18 May 2011 - 01:51 PM Rate Topic: -----

#1 alibahaloo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-May 10

Split string after a specific number of characters

Posted 18 May 2011 - 11:49 AM

Hello guys,
I'm working on a console app, I want to tokenize the string, and put those tokens into an array. each token should have 8 characters. here's an example:
string s = "0100001001100101";

should be split into two tokens; 01000010 and 01100101
how can i achieve this?

Thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Split string after a specific number of characters

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9216
  • View blog
  • Posts: 34,612
  • Joined: 12-June 08

Re: Split string after a specific number of characters

Posted 18 May 2011 - 12:07 PM

The substring method works pretty well (out of the string namespace).
http://msdn.microsof...v=VS.90%29.aspx

The gist is one of the methods allows you to use a 'starting index' and 'length'. It returns that desired string.

From there you can call it again - this time the starting index being the character after the previous starting index plus length to the end or what ever next delimiter you want!

dim foo as string = "abc123"

console.writeline(foo.substring(0,3)) '-- should output abc
console.writeline(foo.substring(3)) '-- should output 123

This post has been edited by modi123_1: 18 May 2011 - 12:08 PM

Was This Post Helpful? 2
  • +
  • -

#3 alibahaloo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-May 10

Re: Split string after a specific number of characters

Posted 18 May 2011 - 12:38 PM

something like this does it, eh?
string s = "010000100110010101110111";
            for (int i = 0; i < s.Length ; i = i + 8)
            {
                Console.WriteLine(s.Substring(i,8));
            }

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9216
  • View blog
  • Posts: 34,612
  • Joined: 12-June 08

Re: Split string after a specific number of characters

Posted 18 May 2011 - 12:48 PM

Looks promising. How did that run for you?

(sorry for the VB.NET code - I was in a frame of mind and forgot to make the jump)
Was This Post Helpful? 0
  • +
  • -

#5 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: Split string after a specific number of characters

Posted 18 May 2011 - 01:51 PM

View Postalibahaloo, on 18 May 2011 - 08:38 PM, said:

something like this does it, eh?
string s = "010000100110010101110111";
            for (int i = 0; i < s.Length ; i = i + 8)
            {
                Console.WriteLine(s.Substring(i,8));
            }

That will do what you want. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1