943,911 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 943
  • C++ RSS
Sep 30th, 2008
0

.rfind for Managed

Expand Post »
I am using this in the std:: to check for the first occurance of " " backwards (rfind).
If I go managed .NET, I wonder if there is any simular method for this
using str->


C++ Syntax (Toggle Plain Text)
  1. std::string::size_type index2 = str.rfind(" ", index - 1);
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Sep 30th, 2008
1

Re: .rfind for Managed

Presumably by go managed .NET you mean use the String class instead of std::string. If so, the LastIndexOf method is what you're looking for.

On a side note, do you even know that MSDN exists, or are you using the people on Daniweb as your personal programming reference?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 30th, 2008
0

Re: .rfind for Managed

Yes I know that MSDN exists but often the examples and info are not so clear there.
I did look up all members for the String^ before and also found LastIndexOf but as I understand this does not have the same function as .rfind.

LastIndexOf(MSDN):
Reports the index position of the last occurrence of a specified Unicode character or String within this instance.
So If you will find the first " " by looking backwards in a string with a startindex: 3 with .rfind, it will look like this:
C++ Syntax (Toggle Plain Text)
  1. std::string str;
  2. str = "1 23 56"
  3. int index = str.rfind(" ", 3); //index should return 1

If you will use LastIndexOf you are looking from the beginning of the string and not backwards.
So index will return: 2 here.

C++ Syntax (Toggle Plain Text)
  1. String^ tot = "1 23 56;
  2.  
  3. int index = 0;
  4. index = tot->LastIndexOf(" "); //Returns 4
  5.  






Click to Expand / Collapse  Quote originally posted by Narue ...
Presumably by go managed .NET you mean use the String class instead of std::string. If so, the LastIndexOf method is what you're looking for.

On a side note, do you even know that MSDN exists, or are you using the people on Daniweb as your personal programming reference?
Last edited by Jennifer84; Sep 30th, 2008 at 3:49 pm.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Sep 30th, 2008
1

Re: .rfind for Managed

>as I understand this does not have the same function as .rfind.
Yes, yes it does.

>int index = str.rfind(" ", 3); //index should return 1
Correct.

>index = tot->LastIndexOf(" "); //Returns 4
Correct, but this isn't an equivalent call to the rfind call you compared with. If you use the overload of LastIndexOf that takes a starting index, and make that starting index 3, you'll find that it returns 1 as well:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace System;
  5.  
  6. int main()
  7. {
  8. std::string s1 = "1 23 56";
  9. String^ s2 = "1 23 56";
  10.  
  11. Console::WriteLine( s1.rfind ( " ", 3 ) );
  12. Console::WriteLine( s2->LastIndexOf ( " ", 3 ) );
  13. }
>If you will use LastIndexOf you are looking from the beginning of the string and not backwards.
I fail to see how you could think that when the documentation explicitly says this (for String::LastIndexOf(String)):
Quote ...
The search begins at the last character position of this instance and proceeds backward toward the beginning until either value is found or the first character position has been examined.
And this (for String::LastIndexOf(String, Int32)):
Quote ...
The search begins at the startIndex character position of this instance and proceeds backward toward the beginning until either value is found or the first character position has been examined. For example, if startIndex is Length - 1, the method searches every character from the last character in the string to the beginning.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 30th, 2008
0

Re: .rfind for Managed

Yes it did look backwards , I didn´t think it did. I red about LastIndexOf here
I couldn´t find here where it described..
>> The search begins at the last character position of this instance and proceeds backward..

This helped great, Thank you !
Last edited by Jennifer84; Sep 30th, 2008 at 6:19 pm.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Oct 1st, 2008
1

Re: .rfind for Managed

>I red about LastIndexOf here
Click on one of the overloads for more details.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 1st, 2008
0

Re: .rfind for Managed

yes you are right. I found the text inside. I should have looked there.
Thanks...

Click to Expand / Collapse  Quote originally posted by Narue ...
>I red about LastIndexOf here
Click on one of the overloads for more details.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Temperature Problem
Next Thread in C++ Forum Timeline: Find highest value in List





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC