Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

parsing string / dynamic array

Posted on 2008-10-12
9
Medium Priority
?
846 Views
Last Modified: 2013-12-17
have a string for example:

string MySentence;

What I want to be able to do is parse that sentence and seperate all the words/entries that are seperated by a space into a seperate mini string/char array..

so,  "this is my first sentence." would become:

[1]  "this"
[2]  " "
[3]  "is"
[4]  " "
[5]  "my"
[6]  " "
[7]  "first"
[8]  " "
[9]  "sentence."

the reason I mention dynamic is that it is not possible to determine how many mini chunks are needed in advance -- depends what is contained in the string initially.

how is type of variable truncating / dynamic array scripted in c# / .net based pages?
0
Comment
Question by:amillyard
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 10

Accepted Solution

by:
kukno earned 960 total points
ID: 22698028
       string sample = "this is my first sentence.";
        string[] words = sample.Split(' ');
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }

Cheers
Kurt
0
 
LVL 10

Assisted Solution

by:kukno
kukno earned 960 total points
ID: 22698040
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 160 total points
ID: 22698132
After seeing what you actually need is not completely provided by string.split() method, but what it gives is the dynamic array but not including the spaces as needed by you

I recommend you to use the string.split() method but just keep in mind that every array member will be followed by a space character. Its just a programming logic and can be implemented quite easily
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 880 total points
ID: 22698267
I am doing a little change to the code by kukno

string sample = "this is my first sentence.";
string sample1 = sample.Replace(" ", "* *");
        string[] words = sample1.Split('*');
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }
0
 

Author Comment

by:amillyard
ID: 22698363
ok, got it -- works fine as written.

utilising one the example (via the url link provided).

is there a way to still have the 'chars' that have been stripped away to be kept?

i.e. ' '   '.'   ','  etc... to be recorded on the next line (like the words seperated.
string inputValue = "this is my first sentence.";
 
            string[] inputWords = 
                inputValue.Split(new char[] { ' ', ',', ';', '.', '-', '/', '|', '\\',
                                              '%', '#', '@', '!', '~', '$', '^', '(',
                                              ')', '+', '[', ']', '{', '}', '"', ':', 
                                              '<', '>', '?',  '\r', '\n', '*' }, StringSplitOptions.RemoveEmptyEntries);
 
            foreach (string inputWord in inputWords)
            {
                Console.WriteLine(inputWord);
            }

Open in new window

0
 

Author Comment

by:amillyard
ID: 22698391
checking through :  ragi0017  &  TechTiger007 submissions (did notice were there, as page was not refreshed in time -- when I submitted my response to kukno
0
 

Author Comment

by:amillyard
ID: 22698406
TechTiger007:  what is the addiitonal value provided with your update please?   i.e. replacing the ' ' with '*' etc.
0
 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 880 total points
ID: 22698438
the space is replaced with *space* and then split the content with * so that you will not loose the space and you will get the data as you wanted

[1]  "this"
[2]  " "
[3]  "is"
[4]  " "
[5]  "my"
[6]  " "
[7]  "first"
[8]  " "
[9]  "sentence."
0
 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 880 total points
ID: 22698451
I used * assuming that your data will not contain * in it. You can use any character or character sequence in place of * only thing to be considered is that your regular data should not consist of this sequence
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question