How to split a string correctly

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

How to split a string correctly

Postby Lena » Tue Dec 20, 2016 2:49 am

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
***
?
Lena
BCBJ Master
BCBJ Master
 
Posts: 522
Joined: Sun Feb 06, 2011 1:28 pm

Re: How to split a string correctly

Postby gtokas » Tue Dec 20, 2016 10:16 am

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!!
"Father is C++ Builder. I'm C++ Killer"
Vangelis Tokas.
12 years old.
gtokas
BCBJ Editor
BCBJ Editor
 
Posts: 78
Joined: Mon Feb 13, 2006 4:41 pm
Location: Thessaloniki Greece

Re: How to split a string correctly

Postby Lena » Wed Dec 21, 2016 3:06 am

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!
Lena
BCBJ Master
BCBJ Master
 
Posts: 522
Joined: Sun Feb 06, 2011 1:28 pm

Re: How to split a string correctly

Postby gtokas » Wed Dec 21, 2016 9:58 am

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.
"Father is C++ Builder. I'm C++ Killer"
Vangelis Tokas.
12 years old.
gtokas
BCBJ Editor
BCBJ Editor
 
Posts: 78
Joined: Mon Feb 13, 2006 4:41 pm
Location: Thessaloniki Greece

Re: How to split a string correctly

Postby Lena » Sun Dec 25, 2016 3:57 am

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?
Lena
BCBJ Master
BCBJ Master
 
Posts: 522
Joined: Sun Feb 06, 2011 1:28 pm

Re: How to split a string correctly

Postby pidgi2009 » Sun Dec 25, 2016 10:42 pm

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
pidgi2009
BCBJ Veteran
BCBJ Veteran
 
Posts: 53
Joined: Sat Apr 25, 2009 11:44 am

Re: How to split a string correctly

Postby Lena » Mon Dec 26, 2016 1:11 am

Thank you!
Lena
BCBJ Master
BCBJ Master
 
Posts: 522
Joined: Sun Feb 06, 2011 1:28 pm

Re: How to split a string correctly

Postby pidgi2009 » Mon Dec 26, 2016 8:45 am

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
pidgi2009
BCBJ Veteran
BCBJ Veteran
 
Posts: 53
Joined: Sat Apr 25, 2009 11:44 am


Return to Technical

Who is online

Users browsing this forum: No registered users and 9 guests

cron