.rfind for Managed

Thread Solved

Join Date: Feb 2008
Posts: 518
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

.rfind for Managed

 
0
  #1
Sep 30th, 2008
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->


  1. std::string::size_type index2 = str.rfind(" ", index - 1);
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,287
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 816
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: .rfind for Managed

 
1
  #2
Sep 30th, 2008
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?
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 518
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: .rfind for Managed

 
0
  #3
Sep 30th, 2008
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:
  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.

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






Originally Posted by Narue View Post
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 2:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,287
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 816
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: .rfind for Managed

 
1
  #4
Sep 30th, 2008
>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:
  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)):
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)):
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.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 518
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: .rfind for Managed

 
0
  #5
Sep 30th, 2008
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 5:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,287
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 816
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: .rfind for Managed

 
1
  #6
Oct 1st, 2008
>I red about LastIndexOf here
Click on one of the overloads for more details.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 518
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: .rfind for Managed

 
0
  #7
Oct 1st, 2008
yes you are right. I found the text inside. I should have looked there.
Thanks...

Originally Posted by Narue View Post
>I red about LastIndexOf here
Click on one of the overloads for more details.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 789 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC