.NET C# stringtok equal to Java.

Posted on 2006-03-22
Last Modified: 2008-02-01
Hi Expert:

I am creating a string with a list of words and each word is to be separated by a comma (name1,house,doe,truck,work etc...).

I don't know actually how many words will be in the list until the end of the function.

I worked something up ( stringvar+= ", ") to put a comma in between each word.

But in the case when function ends with only one word in the list , I get a comma after that one word which is not what I want!

I know I can strip the comma off the one word string at the end of function but how do the experts do this whole thing?

Question by:RSSIAdmin
    LVL 25

    Assisted Solution

    I usually add the comma if its not the last item in the loop like:

    string strtemp = "";
    for (int i = 0;i<10;i++)
      strtemp += i.ToString();
      if (i < 10)
         strtemp += ",";  

    LVL 12

    Accepted Solution

    For a start I would use the System.Text.Stringbuilder to build up your string - I think I'm right when I say that it is a LOT more memory efficient if you are concatenating a lot of strings together. The System.String is immutable; i.e. methods that appear to modify it just return a new instance of it. The StringBuilder, however, is mutable.

    Essentialy stringvar += "newword," creates an entirely new string in memory each time, it doesn't just add the new part to the end of the existing one - baaaaaad. So this uses lots and lots of strings in memory (I've forgotten how to count past 5):
      string stringvar = "a";
      string word2 = "b";
      stringvar += ",";
      stringvar += word2;

    Instead, we can do:
       StringBuilder strbString = new StringBuilder;
       <pseudocode>foreach string word in wordsource.words</p>
          strbString.AppendFormat("{0},", word);
       // now remove the last comma from the stringbuilder
       // and then get the string
       string mystringlist = strbString.ToString();

    Where is your word list coming from by the way? Is it some kind of object array so you know the length? If so, then yes, you can miss off the last comma.


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now