| | |
.rfind for Managed
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 518
Reputation:
Solved Threads: 1
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->
If I go managed .NET, I wonder if there is any simular method for this
using str->
C++ Syntax (Toggle Plain Text)
std::string::size_type index2 = str.rfind(" ", index - 1);
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?
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.
•
•
Join Date: Feb 2008
Posts: 518
Reputation:
Solved Threads: 1
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:
If you will use LastIndexOf you are looking from the beginning of the string and not backwards.
So index will return: 2 here.
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)
std::string str; str = "1 23 56" 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)
String^ tot = "1 23 56; int index = 0; index = tot->LastIndexOf(" "); //Returns 4
•
•
•
•
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.
>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:
>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)):
And this (for String::LastIndexOf(String, Int32)):
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)
#include <iostream> #include <string> using namespace System; int main() { std::string s1 = "1 23 56"; String^ s2 = "1 23 56"; Console::WriteLine( s1.rfind ( " ", 3 ) ); Console::WriteLine( s2->LastIndexOf ( " ", 3 ) ); }
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.
•
•
•
•
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.
•
•
Join Date: Feb 2008
Posts: 518
Reputation:
Solved Threads: 1
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 !
, I didn´t think it did. I red about LastIndexOf hereI 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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Temperature Problem
- Next Thread: Find highest value in List
Views: 789 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm array arrays assignment basic beginner binary c++ c++borland c/c++ calculator char class classes client code compile compiler console constructor conversion convert count delete dll dynamic encryption error file files forms fstream function functions game givemetehcodez graph gui homework iamthwee input int lazy link linker list loop loops map math matrix member memory multidimensional network newbie number numbers object objects opengl output pointer pointers problem program programming project qt random read recursion recursive reference return search sort spoonfeeding string strings struct student studio system template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock wxwidgets






