Results 1 to 18 of 18

Thread: Pascal Cheat Sheet - Data Types : Strings

  1. #1
    Join Date
    Apr 2008
    Posts
    89
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Post Pascal Cheat Sheet - Data Types : Strings

    Strings
    Strings are made for holding text.
    Example:
    SCAR Code:
    var
      myString: String;
    begin
      myString := 'This is a pascal string!';
    end;

    Big word alert: Concatenation
    Concatenation is the combination of one or more strings to make one longer string.


    Strings can also be concatenated together (connected) with the + symbol.
    SCAR Code:
    WriteLn('String 1 '+'String 2'); // This will write: String 1 String 2
    or
    SCAR Code:
    var
      str1, str2: String;
    begin
      str1 := 'A';
      str2 := 'B';
      WriteLn(str1+str2); // <- here
    end;
    Sometimes you need to compare two strings to see if they are alike. In these situations you don't always know the casing of each string.
    Say someone provided a variable with the first letter capitalized when you hadn't intended for it.
    SCAR Code:
    Loc[2] := 'Bank'; //Supposed to be 'bank'
    If you try to compare the two,
    SCAR Code:
    if('bank' = 'Bank') then
      WriteLn('true');
    You will find out they are not equal.
    To fix this problem you should always convert both strings you are about to compare like such.
    SCAR Code:
    if(LowerCase('bank') = LowerCase('Bank')) then
      WriteLn('true');

    There are some built in functions used for extracting information from strings. These will return (as a String) the information specified by the function. Some examples are...
    GetNumbers(Text: string): string;
    SCAR Code:
    WriteLn(GetNumbers('AB162hdfh3')); //Will return '1623'
    GetNumbers(Text: string): string;
    SCAR Code:
    WriteLn(GetLetters('123A456B')); //Will return 'AB'
    GetOthers(Text: string): string;
    SCAR Code:
    WriteLn(GetOthers('abcd12345!@#$%'));//Will return '!@#$%''

    Some functions are used to omit information from Strings. Some examples of these are...
    TrimNumbers(Text: string): string;
    SCAR Code:
    WriteLn(TrimNumbers('abcd12345'));//Will return 'abcd''
    TrimLetters(Text: string): string;
    SCAR Code:
    WriteLn(TrimLetters('abcd12345'));//Will return '12345''
    TrimOthers(Text: string): string;
    SCAR Code:
    WriteLn(TrimOthers('a!b@c#d$%'));//Will return 'abcd''

    Sometimes you need to grab a segment of a string.
    copy(s: string; ifrom, icount: LongInt): string;
    SCAR Code:
    copy('chimp', 1, 5); // would return 'chimp'
      copy('chimp', 1, 4); // would return 'chim'
      copy('chimp', 2, 5); // would return 'himp'





    And this is the end of my short strings tutorial. If I think of anything else that should be added I will add it in. Hopefully some new scripters learn a thing or two about Strings.
    Last edited by ape; 07-06-2010 at 07:12 PM.

  2. #2
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default

    FIRST!

    add the functions for removing spaces, numbers, letters etc

    ~shut

    EDIT:
    function GetNumbers(Text: string): string;
    function GetLetters(Text: string): string;
    function GetOthers(Text: string): string;
    function TrimNumbers(Text: string): string;
    function TrimLetters(Text: string): string;
    function TrimOthers(Text: string): string;
    function Capitalize(S: string): string;
    function Uppercase(s: string): string;
    function Lowercase(s: string): string;
    Last edited by Shuttleu; 07-05-2010 at 04:39 PM.

  3. #3
    Join Date
    Apr 2008
    Posts
    89
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Shuttleu View Post
    FIRST!

    add the functions for removing spaces, numbers, letters etc

    ~shut

    EDIT:
    function GetNumbers(Text: string): string;
    function GetLetters(Text: string): string;
    function GetOthers(Text: string): string;
    function TrimNumbers(Text: string): string;
    function TrimLetters(Text: string): string;
    function TrimOthers(Text: string): string;
    Completely slipped my mind, will add them in...

  4. #4
    Join Date
    Jun 2007
    Location
    La Mirada, CA
    Posts
    2,484
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    May want to include what concatenation is. People don't use that word on a daily basis, and if they are new to programming would be clueless...

    Try to stay constant with examples in tutorials. Since you compared 'bank' to 'Bank' with =, you should compare lowerCase('bank') to lowerCase('Bank') instead of throwing string literals in an example and then variables in. Makes things confusing for beginners.

    "Failure is the opportunity to begin again more intelligently" (Henry Ford)


  5. #5
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default

    maybe after a while you could explain all the string functions
    SCAR Code:
    function copy(s: string; ifrom, icount: LongInt): string;
    Returns part of the string (Copy('abcde',2,3) would return 'bcd'.

    function pos(substr, s: string): LongInt;
    Returns position of substring in string. Returns 0 if not found.

    procedure delete(var s: string; ifrom, icount: LongInt);
    Delete part of string.

    procedure insert(s: string; var s2: string; ipos: LongInt);
    Insert s into s2.

    function Between(s1, s2, s: string): string;
    Returns part of string s between substrings s1 and s2.

    function StrGet(var S: string; I: Integer): Char;

    procedure StrSet(c: Char; I: Integer; var s: string);

    function Uppercase(s: string): string;
    Returns the specified string in upper case.

    function Lowercase(s: string): string;
    Returns the specified string in lowercase.

    function Trim(s: string): string;
    Removes spaces from start and end of string.

    function Length(s: string): LongInt;
    Returns string length in characters.

    function Left(Text: string; Count: Integer): string;
    Returns a part of the leftside of a string, the length is specified by Count.

    function Right(Text: string; Count: Integer): string;
    Returns a part of the rightside of a string, the length is specified by Count.

    function Replace(Text, FindStr, ReplaceStr: string): string;
    Replaces all instances of FindStr in Text by ReplaceStr.

    function GetNumbers(Text: string): string;
    Returns all numerical chars found in a string.

    function GetLetters(Text: string): string;
    Returns all alphabetical chars found in a string.

    function GetOthers(Text: string): string;
    Returns all non-numerical and non-alphabetical chars in a string.

    function TrimNumbers(Text: string): string;
    Removes all numerical chars from a string.

    function TrimLetters(Text: string): string;
    Removes all alphabetical chars from a string.

    function TrimOthers(Text: string): string;
    Removes all chars that are not numerical and not alphabetical from a string.

    function Capitalize(S: string): string;
    Will capitalize strings.
    Example:
    hello world => Hello World

    function PosEx(search, s: string; from: Integer): Integer;
    Returns the position of the substring in s searching forwards from from.

    function LastPosEx(search, s: string; from: Integer): Integer;
    Returns the position of the substring in s searching backwards from from.

    function LastPos(search, s: string): Integer;
    Returns the position of the substring in s searching backwards from the end of the string.

    function StartsWith(prefix, s: string): Boolean;
    Returns true if s starts with prefix.

    function EndsWith(suffix, s: string): Boolean;
    Returns true if s ends with suffix.

    function TrimEx(delimiter, s: string): string;
    Trims the specified delimiter from the start and end of the string.

    function RegexPos(Txt, Regex: string): Integer;
    Returns the position of the first found occurence of the regex.

    function FindRegex(Txt, Regex: string): string;
    Returns the string found by the regex.

    function ReplaceRegex(Txt, Regex, ReplaceStr: string): string;
    Replaces all occurences of the found regex by ReplaceStr.

    function Format(s: string; data: array of const): string;
    Formats a string with data.

    function Implode(Glue: string; Pieces: TStringArray): string;
    Implodes strings into 1 string.

    function Explode(Separator, s: string): TStringArray;
    Explodes a string into pieces.

    function ExplodeEx(Separator, s: string; Limit: Integer): TStringArray;
    Explodes a string into pieces with a Limit.

    ~shut

  6. #6
    Join Date
    Apr 2008
    Posts
    89
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by HyperSecret View Post
    May want to include what concatenation is. People don't use that word on a daily basis, and if they are new to programming would be clueless...

    Try to stay constant with examples in tutorials. Since you compared 'bank' to 'Bank' with =, you should compare lowerCase('bank') to lowerCase('Bank') instead of throwing string literals in an example and then variables in. Makes things confusing for beginners.
    I agree, I will fix this.

    maybe after a while you could explain all the string functions
    I will try to find the ones that are most useful as I was trying to keep this tutorial somewhat short and concise.

  7. #7
    Join Date
    Sep 2006
    Posts
    5,219
    Mentioned
    4 Post(s)
    Quoted
    1 Post(s)

    Default

    Not a big deal but this
    Code:
    WriteLn('String 1 '+'String 2'); // This will write: String 1  String 2
    has an extra space
    Code:
    WriteLn('String 1 '+' String 2'); // This will write: String 1  String 2
    WriteLn('String 1'+'  String 2'); // This will write: String 1  String 2
    WriteLn('String 1  '+'String 2'); // This will write: String 1  String 2
    WriteLn('String 1 '+'String 2'); // This will write: String 1 String 2
    WriteLn('String 1'+' String 2'); // This will write: String 1 String 2

  8. #8
    Join Date
    Apr 2008
    Posts
    89
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Boreas View Post
    Not a big deal but this
    Code:
    WriteLn('String 1 '+'String 2'); // This will write: String 1  String 2
    has an extra space
    Code:
    WriteLn('String 1 '+' String 2'); // This will write: String 1  String 2
    WriteLn('String 1'+'  String 2'); // This will write: String 1  String 2
    WriteLn('String 1  '+'String 2'); // This will write: String 1  String 2
    WriteLn('String 1 '+'String 2'); // This will write: String 1 String 2
    WriteLn('String 1'+' String 2'); // This will write: String 1 String 2
    Ill fix that , I originally had an extra space before String 2 but i forgot to delete it in the comments.

  9. #9
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Quoted
    4 Post(s)

    Default

    Good guide, i didnt knew anything under this:

    if(LowerCase('bank') = LowerCase('Bank')) then
    WriteLn('true');


    feel like that noob now xD, always nice to learn something new, but what is the copy command for? it does nothing is it supose to copy a string to clipboard?

  10. #10
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    function copy(s: string; ifrom, icount: LongInt): string;
    Returns part of the string (Copy('abcde',2,3) would return 'bcd'.

    Seems pretty straight forward to me..

  11. #11
    Join Date
    Apr 2008
    Posts
    89
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Copy has nothing to do with the clipboard, it is merely intended for copying specified segments of strings.

  12. #12
    Join Date
    Feb 2009
    Posts
    2,155
    Mentioned
    4 Post(s)
    Quoted
    42 Post(s)

    Default

    Nothing big but you have

    Code:
    GetNumbers(Text: string): string;
    in there in instead of Getletters

  13. #13
    Join Date
    Mar 2006
    Location
    Behind you
    Posts
    3,193
    Mentioned
    61 Post(s)
    Quoted
    63 Post(s)

    Default

    Had some interesting things in here. I haven't seen most of these function used mainstream I'll keep them in mind. I might just come across something that needs them sometime. Nice Tut

    "Sometimes User's don't need the Answer spelled out with Code. Sometimes all they need is guidance and explanation of the logic to get where they are going."

  14. #14
    Join Date
    Jan 2007
    Location
    the middle of know-where
    Posts
    1,308
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    /BUMP!!!!

    Quote Originally Posted by Shuttleu View Post
    SCAR Code:
    function copy(s: string; ifrom, icount: LongInt): string;
    Returns part of the string (Copy('abcde',2,3) would return 'bcd'.

    function pos(substr, s: string): LongInt;
    Returns position of substring in string. Returns 0 if not found.

    procedure delete(var s: string; ifrom, icount: LongInt);
    Delete part of string.

    procedure insert(s: string; var s2: string; ipos: LongInt);
    Insert s into s2.

    function Between(s1, s2, s: string): string;
    Returns part of string s between substrings s1 and s2.

    function StrGet(var S: string; I: Integer): Char;

    procedure StrSet(c: Char; I: Integer; var s: string);

    function Uppercase(s: string): string;
    Returns the specified string in upper case.

    function Lowercase(s: string): string;
    Returns the specified string in lowercase.

    function Trim(s: string): string;
    Removes spaces from start and end of string.

    function Length(s: string): LongInt;
    Returns string length in characters.

    function Left(Text: string; Count: Integer): string;
    Returns a part of the leftside of a string, the length is specified by Count.

    function Right(Text: string; Count: Integer): string;
    Returns a part of the rightside of a string, the length is specified by Count.

    function Replace(Text, FindStr, ReplaceStr: string): string;
    Replaces all instances of FindStr in Text by ReplaceStr.

    function GetNumbers(Text: string): string;
    Returns all numerical chars found in a string.

    function GetLetters(Text: string): string;
    Returns all alphabetical chars found in a string.

    function GetOthers(Text: string): string;
    Returns all non-numerical and non-alphabetical chars in a string.

    function TrimNumbers(Text: string): string;
    Removes all numerical chars from a string.

    function TrimLetters(Text: string): string;
    Removes all alphabetical chars from a string.

    function TrimOthers(Text: string): string;
    Removes all chars that are not numerical and not alphabetical from a string.

    function Capitalize(S: string): string;
    Will capitalize strings.
    Example:
    hello world => Hello World

    function PosEx(search, s: string; from: Integer): Integer;
    Returns the position of the substring in s searching forwards from from.

    function LastPosEx(search, s: string; from: Integer): Integer;
    Returns the position of the substring in s searching backwards from from.

    function LastPos(search, s: string): Integer;
    Returns the position of the substring in s searching backwards from the end of the string.

    function StartsWith(prefix, s: string): Boolean;
    Returns true if s starts with prefix.

    function EndsWith(suffix, s: string): Boolean;
    Returns true if s ends with suffix.

    function TrimEx(delimiter, s: string): string;
    Trims the specified delimiter from the start and end of the string.

    function RegexPos(Txt, Regex: string): Integer;
    Returns the position of the first found occurence of the regex.

    function FindRegex(Txt, Regex: string): string;
    Returns the string found by the regex.

    function ReplaceRegex(Txt, Regex, ReplaceStr: string): string;
    Replaces all occurences of the found regex by ReplaceStr.

    function Format(s: string; data: array of const): string;
    Formats a string with data.

    function Implode(Glue: string; Pieces: TStringArray): string;
    Implodes strings into 1 string.

    function Explode(Separator, s: string): TStringArray;
    Explodes a string into pieces.

    function ExplodeEx(Separator, s: string; Limit: Integer): TStringArray;
    Explodes a string into pieces with a Limit.

    ~shut
    ^ Any For Simba???
    On vacation in NeverLand,
    Code:
    typedef int bool;
    enum { false, true };

  15. #15
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Quote Originally Posted by anonymity View Post
    ^ Any For Simba???
    Most of these are in Simba already

  16. #16
    Join Date
    Jan 2007
    Location
    the middle of know-where
    Posts
    1,308
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Hmm.. alright. I was trying to use
    Simba Code:
    function Length(s: string): LongInt;
    Returns string length in characters.

    function Left(Text: string; Count: Integer): string;
    Returns a part of the leftside of a string, the length is specified by Count.

    function Right(Text: string; Count: Integer): string;
    Returns a part of the rightside of a string, the length is specified by Count.

    function Replace(Text, FindStr, ReplaceStr: string): string;
    Replaces all instances of FindStr in Text by ReplaceStr.
    and was having some troubles. I wasn't sure if they were included in simba
    On vacation in NeverLand,
    Code:
    typedef int bool;
    enum { false, true };

  17. #17
    Join Date
    Dec 2010
    Posts
    808
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Length and Replace are 100% in simba, the other 2 i have not tried.

    -Boom

  18. #18
    Join Date
    Jan 2007
    Location
    the middle of know-where
    Posts
    1,308
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Yea, unfortunately.. the other two are the ones more required..
    On vacation in NeverLand,
    Code:
    typedef int bool;
    enum { false, true };

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •