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->

std::string::size_type index2 = str.rfind(" ", index - 1);

Recommended Answers

All 6 Replies

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?

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:

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.

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?

>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:

#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 ) );
}

>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.

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 red about LastIndexOf here
Click on one of the overloads for more details.

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

>I red about LastIndexOf here
Click on one of the overloads for more details.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.