The SymbolNameExtractor class is designed to process symbol names and extract only the alphabetic characters from them. It is especially useful when you need to clean up symbol names by removing special characters and numbers, keeping only the letters. This can be useful for creating cleaner and more consistent table names(SQLite) or identifiers in your applications.
Class Details
- Constructor: Initializes a new instance of the SymbolNameExtractor class.
- Method: ExtractLettersOnly
- Purpose: To extract only the alphabetic characters (both uppercase and lowercase) from a given symbol name.
- Parameters: symbol_name (string): The original name of the symbol, which may contain letters, numbers, and special characters.
- Returns: A string containing only the alphabetic characters from the symbol_name.
How It Works:
- Initialize an empty string called result to store the cleaned name.
- Loop through each character in the symbol_name.
- Check if the character is a letter ('A' to 'Z' or 'a' to 'z').
- Append the letter to the result if it meets the criteria.
- Return the cleaned string containing only the letters.
- SymbolNameExtractor class code snippet for MetaTrader 5 (MT5) in MQL5::
//+------------------------------------------------------------------+
//| SymbolNameExtractor.mqh |
//| Copyright 2024, Dragan Drenjanin. |
//| drenjanindragan@gmail.com |
//+------------------------------------------------------------------+
[You must be registered and logged in to see this link.] copyright "Copyright 2024, Dragan Drenjanin."
[You must be registered and logged in to see this link.] link "drenjanindragan@gmail.com"
[You must be registered and logged in to see this link.] version "1.00"
//+------------------------------------------------------------------+
class SymbolNameExtractor {
public:
// Konstruktor // Constructor
SymbolNameExtractor() {}
// Metod za ekstrakciju slova iz imena simbola
// Mthod for extracting letters from symbol names
string ExtractLettersOnly(string symbol_name) {
string result = "";
// Prolazimo kroz svaki karakter u imenu simbola
// We loop through each character in the symbol name
for(int i = 0; i < StringLen(symbol_name); i++) {
ushort current_char = StringGetCharacter(symbol_name, i);
// Proveravamo da li je karakter slovo (a-z ili A-Z)
// We check if the character is a letter (a-z or A-Z)
if((current_char >= 'A' && current_char <= 'Z') || (current_char >= 'a' && current_char <= 'z')) {
// Dodajemo slovo u rezultat tako što ga prvo konvertujemo u string
// We add a letter to the result by first converting it to a string
result += StringFormat("%c", current_char); } }
return result; } };
//+------------------------------------------------------------------+
Here's a simple example showing how to use the SymbolNameExtractor class to clean up a symbol name.
- Example Usage:
// Import the class
// Uvezite klasu
#include "SymbolNameExtractor.mqh"
// Create an instance of SymbolNameExtractor
// Napravite instancu SimbolNameEktractor
SymbolNameExtractor extractor;
void OnStart() {
// Example symbol name with special characters
//
//string symbol_name = "#Abercrombie&Fi";
string symbol_name = Symbol();
// Extract letters only
string clean_name = extractor.ExtractLettersOnly(symbol_name);
// Print the results
// Print("Original Symbol Name: ", symbol_name);
Print("Cleaned Symbol Name: ", clean_name);
}
Output:
//Original Symbol Name: #Abercrombie&Fi
Cleaned Symbol Name: AbercrombieFi
Summary
- Purpose: To clean symbol names by removing non-letter characters.
- Usage: Create an instance of the class and call ExtractLettersOnly with the symbol name you want to clean.
Feel free to use and adapt this class for your projects where you must handle and clean symbol names!