How to extract specific symbol/letter or number from a string

Hi,

I would like to extract specific symbol or letter from a string

e.g.
I have a string like below:

symbol_1 = "aa.aaa.";
symbol_2 = "bb..";
symbol_3 = ".ccc.";
....

and I would like extract "." from the strings to achive like below:
"aaaaa"
"bb"
"cc"

Thanks in advance for any idea.

If you want to remove dots of each string completely then use StrReplace() function.

symbol_1 = "aa.aaa.";
symbol_2 = "bb..";
symbol_3 = ".ccc.";

numvars = 3; // this is number of vars

for( i = 1; i <= numvars; i++ ) {
   ///@link http://forum.amibroker.com/t/how-to-extract-specific-symbol-letter-from-a-string/5240/2
   VarSetText( "symbol_" + i, StrReplace( VarGetText( "symbol_" + i), ".", "" ) );
}

printf( "\nFor example, let's check symbol_2: <b>%s</b>", symbol_2 );

100

4 Likes

the problem is solved
Thank you!!!

@fxshrat
How to extract number from string?

symbol_1 = "aa850abc";
symbol_2 = "bbbcd180bc";

result_1 = 850;
result_2 = 180;

There is no inbuilt function for checking for digits (such as isdigit ) in string. But you can make one using AmiBroker ADK.

Then it would look like so

315

2 Likes

Hi @Fossil You can use a simple code like this:

Input = "bbbcd180bc Milosz";
Output = "";

for( i = 0; ( X = StrMid( Input, i, 1 ) ) != ""; i++ )  if( StrFind( "0,1,2,3,4,5,6,7,8,9", X ) ) Output += X;
// Output = StrToNum(Output); // If you want a number, not a string

printf( "Input:  " + Input + "\n\nOutput:  " + Output );

Int1

5 Likes

@fxshrat
Thanks for the detail and +1 more thanks for the snapshot.

Can write StrFind like this way no clue and is not a comma-separated so can’t use StrExtract for that reason loop out of my mind.
As usual whenever you post this code snippet, I learn something new.
Thanks @Milosz.

@Fossil I'm not quite sure if I understand what you mean :wink: but my code seems to be working fine and it's a really simple solution - all logic in just one line... You can easily convert it to a function or run in a loop ..

Initially I thought, that it would be possible to use typeof instead of other languages' IsDigit(), but I didn't manage, so I used the simplest solution instead.
https://www.amibroker.com/guide/keyword/typeof.html

Additionally just a minor change of the upper code:

Input = "bbbcd180bc Milosz";
Output = "";
Digits = "0,1,2,3,4,5,6,7,8,9"; // Or Digits = "0123456789";

for( i = 0; ( X = StrMid( Input, i, 1 ) ) != ""; i++ )  if( StrFind( Digits, X ) ) Output += X;

Output = StrToNum(Output); // If you want a number, not a string
printf( "Input:  " + Input + "\n\nOutput:  " + Output );

8 Likes

@Milosz
You code work great and I :heart: your coding style because it’s simple & easy to understand.
Above quote I was defining my foolishness :rofl:.

1 Like

For what it is worth, the function isdigit() can be very simply coded as follows, two lines, no DLL:

function isdigit( str )
{
	code = Asc( str );
	return code >= '0' AND code <= '9'; 
}

function isdigitatpos( str, npos )
{
	code = Asc( str, npos );
	return code >= '0' AND code <= '9'; 
}

string = "testing 1234";


for( i = 0; i < StrLen( string ); i++ )
{
  char = StrMid( string, i, 1 );
  printf("pos %g, char %s isdigit() returns %g\n", i, char, isdigit( char ) );
}


for( i = 0; i < StrLen( string ); i++ )
{
  printf("pos %g, isdigitatpos() returns %g\n", i, isdigitatpos( string, i ) );
}

@Milosz solution is much better because it does not tell people to build DLL which is wildly unreasonable for such simple thing like isdigit().

12 Likes

@fxshrat sometimes I'm told, that I take things too personally, but the same applies to you. All I was saying is that my solution is a lot simpler and can be used right away. I doubt if yours is very useful to Fossil, because it needs being acquainted with C/C++ and ADK. I was helping Fossil a few days ago (via PM) and I'm not under the impression that he is familiar with such things and able to build DLL on his own.

I am aware of the fact, that you think that people like me - who don't know C++ and ADK are stupid, but there is no reason to write about, because I have already acknowledged your coding skills supremacy on many different occasions.


@tomasz thank you for the example codes! Using Asc() function has crossed my mind in this context, but I haven't tried it. I've choosen already tested solution :wink:

5 Likes

Well said @Milosz. Just a reminder that this forum is not really into "who is the supreme coder" thing. Less-than-ideal codes are perfectly fine and appreciated as long as they get the job done. And last but not least - you definitely underestimate your own coding skills @Milosz

6 Likes

Tomasz, thank you, but I know my place in the line. There are many better coders than me on the forum - we can see their posts every day (unfortunately some of them are not very active).

My role is definitely not to challenge anybody when it comes to coding skills. I'm a trader - not a coder! I'm just trying to help others, learn new stuff and do whatever I can to stimulate the development of AmiBroker :slight_smile:

I have also written it many times - I really appreciate the input of all those users who write informative replies - I have been learning a lot from such posts.

6 Likes

5 posts were split to a new topic: Off-topic discussion

Sorry guys but the exchange that happened in last 5 posts is completely unneeded and does not bring anything to the subject matter. The forum is not a place for 'battle of egos'. I am closing this thread. Your posts have been moved to private messages.