Page 1 of 1

How to split a string correctly

PostPosted: Tue Dec 20, 2016 2:49 am
by Lena
Hi.
In my program I have a strings:
Code: Select all
http://cbuilder.ru/index.php/1/1
http://cbuilder.ru/index.php/1/2
http://cbuilder.ru/index.php/3/1
http://cbuilder.ru/index.php/3/10
http://cbuilder.ru/index.php/3/101
http://cbuilder.ru/index.php/42/8
***

How to correct and easy to read the latest numbers, separated by slashes in these strings and get the following strings:
11
12
31
310
3101
428
***
?

Re: How to split a string correctly

PostPosted: Tue Dec 20, 2016 10:16 am
by gtokas
You can use SplitString and assign the return array to a TStringList.
Each item inside the string list is a sub string ended by the seperator.
The last 2 items in the list is what you want.

George Tokas.

P.S. MERRY CHRISTMASS AND HAPPY NEW YEAR!!

Re: How to split a string correctly

PostPosted: Wed Dec 21, 2016 3:06 am
by Lena
Thank you!
I also want to try a different way to. How can I break this string with the TRegEx?

MERRY CHRISTMASS AND HAPPY NEW YEAR!

Re: How to split a string correctly

PostPosted: Wed Dec 21, 2016 9:58 am
by gtokas
As I saw it can be done the same way.
There is a function in TRegEx:
static System::DynamicArray<System::UnicodeString> __fastcall Split(const System::UnicodeString Input, const System::UnicodeString Pattern)/* overload */;
Also you can assign the pattern and the options upon creating the object and in this case you don't need the Patern parameter.
As I saw both TRegEx and SplitString are giving the same result. Why bother creating and destroying a TRegEx when there is something you can use without adding anything?

George Tokas.

Re: How to split a string correctly

PostPosted: Sun Dec 25, 2016 3:57 am
by Lena
In our group it was declared competiton.
According to the contest it is necessary to find the most reliable and valid code.
My version is now turned as follows:
Code: Select all
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 String Link = L"http://welcome.um.la/index.php/2/1";
 String URLLink = Link + L".png"; //full link for test, need: 21.png
 TStringDynArray arr(SplitString(URLLink, "/"));
 int j = arr.Length - 1;
 if(j != 0)
   {
   String Result = Trim(arr[j - 1] + arr[j]);
   ShowMessage(Result); //<- 21.png
   }
 }

Can prompt gurus the best variant?

Re: How to split a string correctly

PostPosted: Sun Dec 25, 2016 10:42 pm
by pidgi2009
What would you think of this:

String ConvertLink2Number(String string)
{
std::auto_ptr<TStringList>stringlist( new TStringList());
stringlist->Delimiter = '/';
stringlist->DelimitedText = string;
if (stringlist->Count > 1)
return(stringlist->Strings[stringlist->Count - 2] + stringlist->Strings[stringlist->Count - 1]);
}

Pierre

Re: How to split a string correctly

PostPosted: Mon Dec 26, 2016 1:11 am
by Lena
Thank you!

Re: How to split a string correctly

PostPosted: Mon Dec 26, 2016 8:45 am
by pidgi2009
Sorry. More solid and foolproof is:

String ConvertLink2Number(String string)
{
std::auto_ptr<TStringList>stringlist( new TStringList());
if (stringlist.get()
{
stringlist->Delimiter = '/';
stringlist->DelimitedText = string;
if (stringlist->Count > 1)
return(stringlist->Strings[stringlist->Count - 2] + stringlist->Strings[stringlist->Count - 1]);
}
return "";
}

Pierre