0

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

Edited by Lukezzz: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by thines01
0

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

Edited by thines01: clarity

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.