| | |
.rfind for Managed
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 517
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?
I'm here to prove you wrong.
•
•
Join Date: Feb 2008
Posts: 517
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 3: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.
I'm here to prove you wrong.
•
•
Join Date: Feb 2008
Posts: 517
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 6:19 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Temperature Problem
- Next Thread: Find highest value in List
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






