CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2011
    Location
    Vilnius/Utena, Lithuania
    Posts
    14

    [RESOLVED] string positions

    I have file looking something like this
    Code:
    qwe||$|aa|$|bgb|$|
    asd||$|bab|$|adw|$|
    zxc||$|ccd|$|wda|$|
    This code should is supposed to show me a messagebox with content of strings between "|$|",

    for example:
    Code:
    bab
    adw
    But its giving weird output. I'm guessing it has to do something with positions, but dunno what.

    Code:
    String ^ message;
    string part;
    size_t pos, pos2;
    pos = line.find("|$|");
    while (pos < line.rfind("|$|"))
    {
    	pos2 = line.find("|$|", pos+3);
    	part = line.substr(pos+3, pos2);
    	pos = pos2;
    	message = String::Concat(message, strToStr(part),"\n");
    }
    								
    MessageBox::Show(message, "You have");
    btw... strToStr() is converting from std::string to String and vice versa.
    Last edited by Migeria; January 19th, 2011 at 04:42 PM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: string positions

    Your code is a wild mixture of native C++ and C++/CLI. While this is possible, it is not recommended unless definitely necessary, and I don't see the necessity for that in your code.

    The .NET System::String class offers a wealth of useful methods you can use here. I managed to accomplish your task (including display of the message box) in just four lines of pure C++/CLI code using the System::String methods Split() and Join().

    Hint: For the Split() call you'll need a String delimiter array containing only "|$|" as its sole element. This is tricky (if even possible at all) to write inline in the function call. Therefore I defined it as a variable, constructed using this line:

    Code:
      array<String ^> ^astrDelim = {"|$|"};
    BTW, you don't need your own function to convert native strings to System::String objects. The String class has two constructors (one for Unicode strings and one for ANSI strings) that do that for you.

    BTW2, it would have been helpful to actually describe or show the "weird output" you got. That way I probably wouldn't have been forced to follow the logic of your code (wich is somewhat convoluted) or compile your code (which wouldn't have been easy because it's no complete compilable example). Actually I did non of those; instead I solved the problem from scratch using pure C++/CLI.
    Last edited by Eri523; January 19th, 2011 at 05:55 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jan 2011
    Location
    Vilnius/Utena, Lithuania
    Posts
    14

    Re: string positions

    And that was soooooo simple (~_~) thanks, that worked perfectly (^-^)b

    After getting array from line, I wanted to get rid of 0 dimension (or is it element??? well, first part of after Split()).
    I tried to use Join(String, String[], Int32, Int32), but I couldn't get index of last dimension in array (I tried to use Rank, but that probably was wrong choice =D) so I just used.
    Code:
    result[0]=""; // it's better for later if it doesn't have value
    String ^ result2 = String::Join("\n", result);
    result2 = result2->Remove(0, 1); // to get rid of \n after Join()
    but I don't feel right doing it this way xD Update: seem I will need only first line and others can be excluded from code

    as for "weird output", it would be hard to explain xD
    if I have line
    asd||$|c:/asd/qwe/zxc|$|
    output would be
    c:/asd/qwe/zxc|$|
    if
    asd||$|c:/asd/qwe/zxc|$|c:/asd/qwe/zxc|$|
    output
    c:/asd/qwe/zxc|$|c:/a
    c:/asd/qwe/zxc|$|
    if
    asd||$|c:/asd/qwe/zxc|$|c:/asd/qwe/zxc|$|c:/asd/qwe/zxc|$|
    output
    c:/asd/qwe/zxc|$|c:/a
    c:/asd/qwe/zxc|$|c:/asd/qwe/zxc (I don't remember how exactly this line looked like, but it was longest =D)
    c:/asd/qwe/zxc|$|

    But well, I don't need fix for that "wild mixture" anymore. Your way of approach is much better
    Last edited by Migeria; January 20th, 2011 at 03:17 PM.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: string positions

    Quote Originally Posted by Migeria View Post
    After getting array from line, I wanted to get rid of 0 dimension (or is it element??? well, first part of after Split()).
    I tried to use Join(String, String[], Int32, Int32), but I couldn't get index of last dimension in array (I tried to use Rank, but that probably was wrong choice =D) so I just used.
    Well, you had the right idea, but the Rank property was indeed the wrong choice. The array class has a Length property that can be used here.

    I solved it like this, using the Join() overload that you suggested and the array's length:

    Code:
      String ^strIn = "asd||$|bab|$|adw|$|";
    
      array<String ^> ^astrDelim = {"|$|"};
      array<String ^> ^astrOut = strIn->Split(astrDelim, StringSplitOptions::RemoveEmptyEntries);
      String ^strOut = String::Join("\n", astrOut, 1, astrOut->Length - 1);
      MessageBox::Show(strOut, "You have");
    That way I didn't need to blank out the first element of the split array and remove the bogus '\n' at the beginning of the string resulting from the Join().

    as for "weird output", it would be hard to explain xD
    [...]
    Oh, that looks really chaotic. Luckily we don't need to find out anymore how your code could produce that...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jan 2011
    Location
    Vilnius/Utena, Lithuania
    Posts
    14

    Re: [RESOLVED] string positions

    ah.. I thought Array::Length returns how many symbols there are in whole array xD so I excluded it from possible variants.
    Seems I mixed up what is element and what's dimension (~_~')

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured