String'ler / String ManipulationStrings / String Manipulation
String, karakter/rakam dizisinin bir türüdür ve programlama dillerinin en yaygın type'larından biridir. Bu bölüm, string type'ının bazı key feature'larını ve en çok kullanılan string manipulation function'larını tanıtmayı amaçlar.String is a kind of sequence of characters/digits and it is one of most common types of programming languages. This section aims to introduce some key features of string type and most used string manipulation functions.
String'ler Hakkında Bazı GerçeklerSome Facts About Strings
TROIA'da, STRING type java gibi diğer programlama dillerine kıyasla hem string hem char type için kullanılır. String type variable'ların ve hardcode string'lerin hard-coded bir maksimum uzunluğu (character-limit) yoktur, bu yüzden maksimum karakter uzunluğuyla ilgili bir sorununuz varsa muhtemelen bu database table'ınızın, table column'ınızın veya editor uzunluğunuzla ilgilidir.In TROIA, STRING type is used for both string and char type compared to other programming languages such as java etc. There is not hard-coded maximum length (character-limit) of string typed variables and hard code strings, so if you have trouble about maximum character length, possibly its about your length of your database table, table column or editor length etc.
String variable'lar her türlü karakteri ve rakamı saklayabilir.String variables are able to store all kind of characters and digits.
Önceki bölümlerde belirtildiği gibi, undefined variable'lar kendi isimlerini döndürür. STRING variable gibi görünseler de undefined variable'lar string değildir, sadece kendi isimlerini saklayan undefined symbol'lerdir.As mentioned previous sections, undefined variables are returns their name. It seems they are STRING variables but undefined variable’s are not string, they are just undefined symbols that stores their name.
Tek tırnak karakteri (') TROIA code'unda hardcode string'leri sınırlamak için kullanılır.Single quote character (’) is used to limit hard code strings in TROIA code.
Escaping & Special Character'larEscaping & Special Characters
Bir hardcode string'e anlamlı bir karakter (örneğin ') eklemek için farklı programlama dillerinde escape character kullanılır. Escape character, sonraki karakterlerin yorumlanma yöntemini değiştirir. Örneğin ' (tek tırnak) karakteri hardcode string'leri sınırlar; programcı tek tırnak eklemek istediğinde bir escape character kullanmalıdır.To insert a meaningful character (such as ‘) to a hard code string, escape character are used in different programming languages. Escape character changes the interpretation method of following characters. For example ‘ (single quote) character limits hard code strings, when programmer wants to insert a single quote, he/she must use an escape character.
TROIA, hardcode string'ler için escape character desteklemez. Escaping yerine, bir hardcode string'e special character eklemek için TOCHAR() system function'ı kullanılır. TOCHAR() function, istenen karakterin decimal ASCII karşılığını alır ve string olarak tek bir karakter döndürür. Örneğin programcı tek tırnak karakteri için 39, newline karakteri için 10 geçirmelidir.TROIA does not support escape characters for hard code strings. Instead of escaping, TOCHAR() system function is used to insert special characters to a hard code string. TOCHAR() function gets decimal ASCII correspondent of requested character and returns a single character as string. For example programmer must pass 39 to get a single quote character or 10 for a newline character.
OBJECT:
STRING STRVAR;
STRVAR = 'First line, it contains ' + TOCHAR(39) + ' (single quote).';
STRVAR = STRVAR + TOCHAR(10) + 'Second line.';
/*
value of STRVAR must like:
First line, it contains ' (single quote).
This is second line.
*/Basic String Function'larıBasic String Functions
İşte string variable'lar hakkında temel bilgi döndüren system function'lar:Here is system functions that returns basic information about string variables:
| STRLEN | Returns the length of given string |
|---|---|
| STRPOS() | Returns index of given string inside another |
| ISNUMERIC() | Check string whether all characters are numeric |
| STRLIKE() | Compares string with given pattern like SQL LIKE op. |
ve string'leri değiştirmek için en çok kullanılan system function'lar:and most used system functions to modify strings:
| TRIM() | Removes white spaces from the beginning and the end. |
|---|---|
| REPLACE() | Replaces a given string with another |
| STRSTR() | Returns substring of string with index and length |
| LOWERCASE() | Returns lowercase of given string with given language |
| UPPERCASE() | Returns uppercase of given string with given language |
Daha fazla string manipulation function'ı veya bu function'lar hakkında daha fazla bilgi (parameter sırası ve return type'ları gibi) için lütfen TROIA Help'in String Manipulation bölümüne bakınız.For more string manipulation functions or more information about these functions (like parameter order and return types etc), please see String Manipulation section of TROIA Help.
Base64 Encoding/DecodingBase64 Encoding/Decoding
Base64, binary data'yı bir ASCII string formatında temsil eden bir tür binary-to-text encoding scheme'idir. Ortaya çıkan string yalnızca a-z, A-Z, 0-9, +, / karakterlerini içeren 64 türde karakter içerir, bu yüzden ortaya çıkan string 'yalnızca ASCII' environment'larında saklanabilir veya aktarılabilir.Base64 is a kind binary-to-text encoding scheme that represent binary data in an ASCII string format. Resulting string contains only 64 kind of characters which contains only a-z, A-Z, 0-9, +, / characters so the resulting string can be stored on or transferred between ‘ASCII only’ environments.
String'leri Base64'e encode etmek için BASE64ENCODE() system function'ı, bir base64 string'i troia string'ine dönüştürmek için BASE64DECODE() system function'ı kullanılır. Hem encoding hem decoding, string'i binary'ye veya binary'yi string'e dönüştürmek için 'UTF-8' gibi bir encoding gerektirir. BASE64ENCODE() & BASE64DECODE() function'ları hakkında daha fazla bilgi için lütfen TROIA help dokümanlarına bakınız.To encode strings to Base64 BASE64ENCODE() system function is used, to convert a base64 string to a troia string BASE64DECODE() system function is used. Both encoding and decoding requires an encoding like ‘UTF-8’ etc to convert string to binary or binary to string. For more information about BASE64ENCODE() & BASE64DECODE() functions please see TROIA help documents.
İşte bir encoding ve decoding örneği:Here is an encoding and decoding example:
OBJECT:
STRING STRVALUE,
STRING STRBASE64,
STRING STRDECODED;
STRVALUE = 'Programming With TROIA';
STRBASE64 = BASE64ENCODE(STRVALUE, 'UTF-8');
STRDECODED = BASE64DECODE(STRBASE64, 'UTF-8');Hashing String'lerHashing Strings
Hashing, herhangi bir boyuttaki data'yı sabit bir boyuta eşleyen bir işlemdir. Ortaya çıkan hash değeri orijinal data'ya decode edilemez, çünkü farklı input değerlerinin hash değerleri aynı olabilir. Örneğin verilen bir string'in uzunluğu bir tür hashing olarak düşünülebilir. En çok kullanılan hashing algoritmaları MD5, SHA1, MD2 vb.'dir.Hashing is an operation that maps an arbitrary size data to fixed size. Resulting hash value can not be decoded to original data, because hash values of different input values can be identical. For example length of given string can be thought as a kind of hashing. Most used hashing algorithms are MD5,SHA1, MD2 etc.
TROIA'da, birden fazla algoritma kullanarak hash yapmak için GETDIGEST() function'ı kullanılır. Daha fazla bilgi ve desteklenen algoritmalar için lütfen TROIA Help'e bakınız.In TROIA, GETDIGEST() function is used to hash using multiple algorithms. For more information and supported algorithms please see TROIA Help.
OBJECT:
STRING STRVALUE,
STRING MD5HASH,
STRING SHA1HASH;
STRVALUE = 'Programming With TROIA';
MD5HASH = GETDIGEST(STRVALUE, 'MD5');
SHA1HASH = GETDIGEST(STRVALUE, 'SHA1');String ConcatenationString Concatenation
Varsayılan olarak TROIA'da string'leri birleştirmek için + operatörü kullanılır, ancak performans sorunları nedeniyle büyük string'ler oluşturmak için + operatörünü kullanmak önerilmez.As default + operator is used to concatenate strings in TROIA, but it is not recommended to use + operator to build large strings because of performance issues.
OBJECT:
STRING FINALSTRING;
FINALSTRING = 'Hello ' + ' World.';String concatenation'ın performans sorunu yalnızca '+' operatörüyle ilgili değildir, aynı zamanda STRING type'ıyla da ilgilidir. Performans sorunlarını çözmek için, programcılar önceki bölümlerde bahsedilen STRINGBUILDER data type'ını kullanmalıdır. Bir STRINGBUILDER tanımlamak, bir STRING variable tanımlamaktan farklı değildir. Verilen bir string'i bir STRINGBUILDER'a eklemek/concat etmek için APPENDSTRING komutu kullanılır. İşte önceki örneğin STRINGBUILDER & APPENDSTRING karşılığı.Performance problem of string concatenation is not about only ‘+’ operator, its also about STRING type. To solve performance issues, programmers must use STRINGBUILDER data type which is mentioned in previous sections. Defining a STRINGBUILDER is not different from defining a STRING variable. To append/concat a given string to a STRINGBUILDER, APPENDSTRING command is used. Here is STRINGBUILDER & APPENDSTRING equivalent of previous example.
OBJECT:
STRINGBUILDER SBFINALSTRING;
APPENDSTRING 'Hello' TO SBFINALSTRING;
APPENDSTRING ' World' TO SBFINALSTRING;STRINGBUILDER yalnızca yüksek performanslı string building operation'ları için tasarlanmıştır, bu yüzden STRING yerine STRINGBUILDER object kullanmak önerilmez.STRINGBUILDER is designed for only hi-performance string building operations, so it is not recommended to use STRINGBUILDER object instead of STRING.
STRING/+ ve STRINGBUILDER/APPENDSTRING kullanımını karşılaştırmak (benchmark) için bin kez concat operation'ı yapan bir while loop yazabilirsiniz.To benchmark using STRING/+ and STRINGBUILDER/APPENDSTRING you can write a while loop that makes one thousand concat operation.
String'ler Üzerinde Looping: PARSE KomutuLooping on Strings: PARSE Command
Bir string'i token'lara ayırmak (newline veya belirli bir delimiter karakteriyle) ve her token için bir şeyler yapmak için PARSE komutu kullanılır. Görüldüğü gibi PARSE komutu bir tür loop statement'ıdır, bu yüzden BREAK ve CONTINUE statement'ları da parse komutunda çalışır. İşte temel syntax:To split a string into tokens (with newline or with a specific delimiter character) and do something for each token, PARSE command is used. As it is obvious PARSE command is a kind of loop statement, so BREAK and CONTINUE statements also works in parse command. Here is the basic syntax:
PARSE {mainstring} INTO {token} [DELIMITER {delimiter}]
BEGIN
parse block
ENDPARSE;Delimiter opsiyonel bir parametredir; belirtilmezse newline (linefeed, n) karakteri kullanılır ve ana string satırlara bölünür.Delimiter is an optional parameter; if it is not specified newline (linefeed, n) character is used and main string is splitted into lines.
Aşağıdaki örnek her satırın uzunluğunu hesaplamaya çalışır.The sample below tries to calculate length of each line.
OBJECT:
STRING STRMAINSTRING,
STRING STRTOKEN;
OBJECT:
INTEGER NLINECOUNT,
STRING STRRESULT;
NLINECOUNT = 0;
STRRESULT = '';
STRMAINSTRING = 'I do not trust words.';
STRMAINSTRING = STRMAINSTRING + TOCHAR(10) + 'I trust actions.';
PARSE STRMAINSTRING INTO STRTOKEN
BEGIN
NLINECOUNT = NLINECOUNT + 1;
STRRESULT = STRRESULT + 'Line ' + NLINECOUNT + ':' + STRLEN(STRTOKEN);
STRRESULT = STRRESULT + ' char(s).' + TOCHAR(10);
ENDPARSE;Alıştırma 1: Basic String Function'larıExercise 1: Basic String Functions
İşte basit bir string function'ları örneği, lütfen her function çağrısından sonraki return değerlerini bulmaya çalışın.Here is a simple string functions example, please try to find return values after each function call.
OBJECT:
STRING STRMESSAGE,
INTEGER NINDEX;
STRMESSAGE = ' Hello TROIA ' + TOCHAR(10);
/* remove whitespaces */
STRMESSAGE = TRIM(STRMESSAGE);
/* replace word 'TROIA' with 'world' */
STRMESSAGE = REPLACE(STRMESSAGE, 'TROIA', 'World');
/* calculate index of 'world' */
NINDEX = STRPOS(STRMESSAGE, 'World');
/* get first 5 character*/
STRMESSAGE = STRSTR(STRMESSAGE, 0, 5);Alıştırma 2: Numeric Token'ları ListelemeExercise 2: List Numeric Tokens
OBJECT:
STRINGBUILDER SB,
STRING STRNEWLINE,
STRING MAINSTRING,
STRING TOKEN;
SB = '';
STRNEWLINE = TOCHAR(10);
MAINSTRING = '1|A|2|B|3|C|4|D|5|E';
PARSE MAINSTRING INTO TOKEN DELIMITER '|'
BEGIN
IF !ISNUMERIC(TOKEN) THEN
CONTINUE;
ENDIF;
APPENDSTRING TOKEN TO SB;
APPENDSTRING ' is a number.' TO SB;
APPENDSTRING STRNEWLINE TO SB;
ENDPARSE;