I am looking for a way to find the first occurence of: " " when searching backwards in String1 with beginning at index where # was found.

However with LastIndexOf, this will find the Last occurence of " ".
How can I find the first occurence of " " wich is just Before the letter "R" in the string ?

String1^ = " aaa bbb Rcc # ddd";
int index = String1->IndexOf(" # ");

int index2 = String1->LastIndexOf(" ", index - 1);

There are a couple of techniques I would use to get that token.
The best is probably a regular expression (Technique2).
The other does a brute-force Split() twice to get the token.

#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Text::RegularExpressions;

String^ Technique1(String^ strRawData)
{
	array<String^>^ arr_strData1 = strRawData->Split('#');
	
	array<String^>^ arr_strData2 = 
		(arr_strData1[0]->Split(String(" ").ToCharArray(),
			StringSplitOptions::RemoveEmptyEntries));
			
	return arr_strData2[arr_strData2->Length-1];
}

String^ Technique2(String^ strRawData)
{
	Regex^ regEx = gcnew Regex(" [A-Za-z]{1,} #");
	return regEx->Match(strRawData)->Value->Trim()->Replace("#","");
}

int main(array<System::String ^> ^args)
{
	String^ str = " aaa bbb Rcc # ddd";

	Console::WriteLine(Technique1(str));
	Console::WriteLine(Technique2(str));
	return 0;
}
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.