How to Perform String Manipulation In Delphi?

10 minutes read

String manipulation in Delphi is a common task that involves performing various operations on strings, such as concatenation, extraction, searching, and replacing. Delphi provides a wide range of functions and techniques to manipulate strings efficiently. Here are some key points on how to perform string manipulation in Delphi:

  1. Concatenation: Delphi uses the '+' operator to concatenate strings. For example, to concatenate two strings, you can use the following syntax: str := str1 + str2;
  2. Extraction: Delphi provides the Copy function to extract substrings from a larger string. It takes the source string and the starting index as parameters and returns the extracted substring. For example: subStr := Copy(str, startPos, length);
  3. Searching: Delphi offers several functions to search for substrings within a string. The most commonly used function is Pos, which returns the starting index of the first occurrence of a substring within a string. For example: index := Pos(subStr, str);
  4. Replacing: To replace one substring with another within a string, you can use the StringReplace function. It takes the source string, the string to search for, the replacement string, and additional parameters and returns the modified string. For example: newStr := StringReplace(str, oldSubStr, newSubStr, []);
  5. Conversion: Delphi provides functions like IntToStr, StrToInt, FloatToStr, and StrToFloat to convert between strings and numeric values.
  6. Case conversion: Delphi offers functions like LowerCase and UpperCase to convert the case of a string.
  7. Splitting and joining: Delphi provides the SplitString function from the System.StrUtils unit to split a string into substrings based on a separator. The JoinString function can be used to join an array of strings into a single string.


These are just some of the basics of string manipulation in Delphi. Delphi also offers numerous other functions and libraries to handle more advanced string operations, such as regular expressions.

Best Delphi Books to Read in 2024

1
Delphi GUI Programming with FireMonkey: Unleash the full potential of the FMX framework to build exciting cross-platform apps with Embarcadero Delphi

Rating is 5 out of 5

Delphi GUI Programming with FireMonkey: Unleash the full potential of the FMX framework to build exciting cross-platform apps with Embarcadero Delphi

2
Delphi Cookbook: Recipes to master Delphi for IoT integrations, cross-platform, mobile and server-side development, 3rd Edition

Rating is 4.9 out of 5

Delphi Cookbook: Recipes to master Delphi for IoT integrations, cross-platform, mobile and server-side development, 3rd Edition

3
Mastering Pascal and Delphi Programming (Palgrave Master Series (Computing), 1)

Rating is 4.8 out of 5

Mastering Pascal and Delphi Programming (Palgrave Master Series (Computing), 1)

4
Delphi Cookbook - Second Edition

Rating is 4.7 out of 5

Delphi Cookbook - Second Edition

5
Delphi Programming Unleashed/Book and Disk

Rating is 4.6 out of 5

Delphi Programming Unleashed/Book and Disk

6
Mastering Delphi Programming: A Complete Reference Guide: Learn all about building fast, scalable, and high performing applications with Delphi

Rating is 4.5 out of 5

Mastering Delphi Programming: A Complete Reference Guide: Learn all about building fast, scalable, and high performing applications with Delphi

7
Programming and Problem Solving with Delphi

Rating is 4.4 out of 5

Programming and Problem Solving with Delphi

8
Delphi Programming for Dummies

Rating is 4.3 out of 5

Delphi Programming for Dummies

9
The Little Book Of Delphi Programming: Learn To Program with Object Pascal (Little Programming Books)

Rating is 4.2 out of 5

The Little Book Of Delphi Programming: Learn To Program with Object Pascal (Little Programming Books)


How to convert a string to uppercase in Delphi?

In Delphi, you can convert a string to uppercase using the UpperCase function. Here's an example:

1
2
3
4
5
6
7
var
  myString: string;
begin
  myString := 'hello world';
  myString := UpperCase(myString);
  ShowMessage(myString); // Output: HELLO WORLD
end;


In this example, the initial value of myString is 'hello world'. We then call the UpperCase function passing myString as the argument. The function returns a new string with all characters converted to uppercase, which we assign back to myString. Finally, we display the converted string using ShowMessage.


How to convert a string to an integer in Delphi?

To convert a string to an integer in Delphi, you can use the StrToInt or StrToIntDef functions. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
var
  str: string;
  num: Integer;
begin
  str := '123'; // the string you want to convert

  // using StrToInt function
  num := StrToInt(str);

  // using StrToIntDef function with default value
  num := StrToIntDef(str, 0); // the second parameter is the default value if the conversion fails

  // use the converted integer here
end;


In the code above, the StrToInt function is used to convert the string str to an integer. If the conversion fails, it will raise an EConvertError exception. If you want to provide a default value in case of a conversion failure, you can use the StrToIntDef function instead, which takes a default value as the second parameter.


How to replace a specific character in a string in Delphi?

To replace a specific character in a string in Delphi, you can use the StringReplace function. Here's an example:

  1. Declare a new string variable to store the modified string.
  2. Use the StringReplace function and provide the original string, the character you want to replace, the replacement character, and the necessary options.
  3. Assign the result of the StringReplace function to the new string variable.


Here's the code:

1
2
3
4
5
6
7
8
9
var
  originalString, modifiedString: string;
begin
  originalString := 'Hello, World!';
  
  modifiedString := StringReplace(originalString, ',', ';', [rfReplaceAll]);
  
  ShowMessage(modifiedString); // Displays 'Hello; World!'
end;


In this example, we replaced the comma character (',') with a semicolon (';') using the StringReplace function. The rfReplaceAll option ensures that all occurrences of the character in the original string are replaced.


How to replace multiple substrings in a string in Delphi?

In Delphi, you can use the StringReplace function to replace multiple substrings in a string. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var
  Str: string;
begin
  Str := 'This is a sample string.';

  // Replace 'is' with 'was' and 'sample' with 'example'
  Str := StringReplace(Str, 'is', 'was', [rfReplaceAll]);
  Str := StringReplace(Str, 'sample', 'example', [rfReplaceAll]);

  ShowMessage(Str); // Output: 'Thwas was a example string.'
end;


In this example, we first replace all occurrences of 'is' with 'was' using the StringReplace function. Then, we replace all occurrences of 'sample' with 'example' in the updated string. The [rfReplaceAll] flag is used to replace all occurrences in the string.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

Passing a string from C++ to Delphi involves converting the string from C++ to a compatible format in Delphi. Here's how you can achieve it:Declare a function in your Delphi code to receive the string parameter. The function should be declared with the std...
If you encounter the Delphi "out of memory" error, there are a few steps you can take to try and resolve it:Check system resources: Ensure that your computer has sufficient memory available for the Delphi application to run. Close any unnecessary progr...
To parse a JSON string response using Delphi, you can use the built-in System.JSON unit that comes with Delphi. Here is a step-by-step guide on how to accomplish this:Start by adding the System.JSON unit to the uses clause of your Delphi unit. This unit contai...