6 Replies - 11038 Views - Last Post: 24 December 2005 - 11:35 PM Rate Topic: -----

#1 rukiaKuchiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-December 05

String Manipulation

Posted 19 December 2005 - 09:03 PM

This is a program I found on the net a few days ago. It's supposed to be used to get each word to be placed on a new line. However, what I am aimed to do is this:

Instead of: I;am;writing;in;C#

I want the output to be:

I
am
writing
in
C#

How and where can I edit the below code so that it can get rid of the ";" and put each word on a new line?
private ArrayList tokens;
private int currIndex;
private int numTokens;
private string currStr;
private string currDelimiter;

public StringTokeniser() {
    
}

public StringTokeniser(string text, string delimiter){
    
    this.tokens = new ArrayList();
    this.currStr = text;
    this.currDelimiter = delimiter;
    
    if(delimiter.Length == 0){
        
        this.currDelimiter = " ";
        
    }
    this.Tokenize();
    
}

public StringTokeniser(string text, char[] delimiter) {
    
}

public StringTokeniser(string source) {
    
}

public string CurrStr{
    
    get{
        
        return this.currStr;
        
    }
}

public string Delimiter{
    
    get{
        
        return this.currDelimiter;
        
    }
}

private void Tokenize() {
    
    string TempSource = this.currStr;
    string Tok = "";
    this.numTokens = 0;
    this.tokens.Clear();
    this.currIndex = 0;
    
    if(TempSource.IndexOf(this.currDelimiter) < 0 && TempSource.Length > 0)
    {
        this.numTokens = 1;
        this.currIndex = 0;
        this.tokens.Add(TempSource);
        this.tokens.TrimToSize();
        TempSource = "";
    }
    else if(TempSource.IndexOf(this.currDelimiter) < 0 && TempSource.Length <=0)
    {
        this.numTokens = 0;
        this.currIndex =0;
        this.tokens.TrimToSize();
    }
    while(TempSource.IndexOf(this.currDelimiter) >= 0){
        
        if(TempSource.IndexOf(this.currDelimiter) == 0)
        {
            if(TempSource.Length > this.currDelimiter.Length)
            {
                TempSource = TempSource.Substring(this.currDelimiter.Length);
            }
            else
            {
                TempSource = "";
            }
        }
        else
        {
            Tok = TempSource.Substring(0,TempSource.IndexOf(this.currDelimiter));
            this.tokens.Add(Tok);
            if(TempSource.Length > (this.currDelimiter.Length + Tok.Length))
            {
                TempSource = TempSource.Substring(this.currDelimiter.Length + Tok.Length);
            }
            else
            {
                TempSource = "";
            }
        }
    }
    if(TempSource.Length > 0)
    {
        this.tokens.Add(TempSource);
    }
    this.tokens.TrimToSize();
    this.numTokens = this.tokens.Count;
}

public void NewSource(string newSrc){
    this.currStr = newSrc;
    this.Tokenize();
}

public void NewDelim(string newDel)
{
    if(newDel.Length == 0)
    {
        this.currDelimiter = " ";
    }
    else
    {
        this.currDelimiter = newDel;
    }
    this.Tokenize();
}

public void NewDelim(char[] newDel){
    
    string temp = new String(newDel);
    if(temp.Length == 0)
    {
        this.currDelimiter = " ";
    }
    else
    {
        this.currDelimiter = temp;
    }
    this.Tokenize();
}

public int CountTokens(){
    
    return this.tokens.Count;
}

public bool HasMoreTokens(){
    
    if(this.currIndex <= (this.tokens.Count - 1)){
        
        return true;
        
        } else{
        
        return false;
        
    }
}

public string NextToken() {
    
    string result = "";
    if(this.currIndex <= (this.tokens.Count - 1)) {
        result = (string)tokens[currIndex];
        this.currIndex++;
        return result;
        
        } else{
        
        return null;
        
    }
}


} 



Hope someone can help. Thanks.

- Rukia

This post has been edited by skyhawk133: 21 December 2005 - 08:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: String Manipulation

#2 Datalus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 235
  • Joined: 18-December 05

Re: String Manipulation

Posted 20 December 2005 - 05:58 AM

In the grand scheme of things, have the delimiter be:
\n

This tells the cursor to go to the next line.
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: String Manipulation

Posted 20 December 2005 - 07:47 AM

Ya think? :)

Can you post the actions you are taking to have the output string generated? What do you enter, and where do you enter it to see that output string? the only reason I ask is becasue thwere is already a NewDelim method (two, actually) that will allow you to change the delimiter.

Also can you post the code you are using to diplay the output?
Was This Post Helpful? 0
  • +
  • -

#4 rukiaKuchiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-December 05

Re: String Manipulation

Posted 20 December 2005 - 06:42 PM

sure thing...there's actually two other classes which go with this class. the class that i've presented to you is the string tokenizer class. The others are still a work in progress. The following class is the class that reads in the .dat file and displays the result. the class after that is the one where it organizes the information. i've actually compiled it with the string tokenizer class and it seems to work the way i want it to...however i've been told that my Tokenize() in the string tokenizer class is a bit too complex. I want to use the split() but i don't where to implement that. oh, also...the string tokenizer and toy classes actually extend interfaces...

using System;
using System.IO;
using System.Collections;

namespace Store {

/// <summary>
/// File - store.cs
/// </summary>

class Store {

//Attributes

static ArrayList listOfToys = new ArrayList();
static string inFile = "toys.dat";

//Constructor
public Store() {

}

//Methods

/* Pre: true
* Post: data has been read, Toy objects created and stored
*/
public static void GetData() {

try {
StreamReader carFile = new StreamReader(inFile);
string readCars = carFile.ReadLine();
StringTokeniser st = new StringTokeniser(readCars, ";");

while(st.HasMoreTokens())
{
try
{
Car car = new Car();
string str = st.NextToken();
listOfCars.Add(st.NextToken());

Console.WriteLine(str);
//car.Price(st.NextToken());
//listOfCars.Add(st.NextToken());
}
catch
{
Console.WriteLine("Car not valid"); }
}


} catch {
Console.WriteLine("Cannot be tokenised");
}

}

/* Pre: numToys > 1
* Post: Toys are sorted according to Toys.IsBefore(), using
* selection sort algorithm
*/
public void SortToys() {


}

/* Pre: true
* Post: catalog of toys is output to screen
*/
public void Output() {


}


[STAThread]
static void Main(string[] args){

GetData();
try {

StreamReader inStream = new StreamReader(inFile);

} catch (Exception e) {

Console.WriteLine("Unable to open {0}", inFile);

} finally {

Console.Write("\nPress ENTER to continue");
Console.Read();
}

//Check for correct program usage

//If correct, continue execution

}
}
}


-----------------------------------------------------------------------------------

using System;

namespace Store
{
/// <summary>
/// Summary description for Toys.
/// </summary>
public class Toys{

private string make;
private string model;
private string type;
private string colour;
private int year;
private string reg;
private string price;
private string desc;

public Toys(){



}

public string Make {
get { return make; }
}

public string Model {
get { return model; }
}

public string Type {
get { return type; }
}

public string Colour {
get { return colour;}
set { colour = value;}
}

public int Year {
get{ return year; }
}

public string Reg {
get{ return reg; }
}

public string Price {
get { return price;}
set { price = value;}
}

public string Desc {
get { return desc;}
set { desc = value;}
}

/*Pre: t != null
*Post:return true if this.Year < t.Year,
* return false if this.Year > t.Year,
* if this.year = t.Year, use Reg likewise
*/
public bool IsBefore(Toys t) {

return true;
}

/*Pre: true
*Post:toy details in formatted string are returned
*/
public override string ToString() {
return String.Format("\nMake: {0}\n Model: {1}\n Type: {2}\n" +
"Colour: {3}\n Year: {4,4}\n Reg: {5}\n Price: {5:F2}\n" +
"Description: {6}", this.Make, this.Model, this.Type, this.Colour,
this.Year, this.Reg, this.Price, this.Desc);
}
}
}

This post has been edited by rukiaKuchiki: 21 December 2005 - 02:16 AM

Was This Post Helpful? 0
  • +
  • -

#5 AdmSteck  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 26-November 05

Re: String Manipulation

Posted 21 December 2005 - 05:39 PM

Seems like an awful lot of work for something that should be rather simple. Couldn't you just parse through the whole file replacing all the ";" with "\n" as Datalus suggested. Also, I'm not sure what the equivalent is in C#, but in vb there is a split function that would serve to split any string into an array using the named delimiter. Something like....

Dim StringToParse As String = "I;am;writing;in;VB"
Dim ParseStringArray As String() = StringToParse.Split(CChar(";"))


If you are trying to parse a file, I'm pretty sure C# has some equivelant to the streamreader class that could read in the file one line at a time and then parse each line. Don't know if this helps.
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: String Manipulation

Posted 21 December 2005 - 06:48 PM

AdmSteck, on 21 Dec, 2005 - 08:36 PM, said:

If you are trying to parse a file, I'm pretty sure C# has some equivelant to the streamreader class that could read in the file one line at a time and then parse each line.

The StreamReader class, as with most classes used in the .NET framework, were actually developed for C# first, and then ported to VB.NET. It is now part of the larger framework library.

It is available in all .NET languages.
Was This Post Helpful? 0
  • +
  • -

#7 rukiaKuchiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-December 05

Re: String Manipulation

Posted 24 December 2005 - 11:35 PM

I've finally got the String Tokenizer class working. Well, it was working before but it was too complicating. But now it's implemented in a simpler way. :) Yes, I did use the Split method...and it works. Now I'm stuck on how to do the SortToys. :S
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1