Saturday, August 10, 2013

Working with Strings


The ‘String’ object represents an array (you can perform any operations that relevant to any other array) of characters that you can use to host any structure of chars.

The string is defined as ‘Object’ and not a simple variable that created on the stack, it’s really a ‘Reference’ type that point to a specific location to values that located on the ‘Heap’.

The uniqueness about the strings is their definition as ‘immutable’, objects that define in that way cannot be changed after their definition (Reference types can be changed after their definition)

To demonstrate the differences I’ll provide 3 examples: 

Example 1 (Non reference type):

As you can see int b remain the same and not affected from the changes, both integers configured on the stuck and configured without any references on the heap.

Example 2 (Reference type):

As you can see, both instances (E1.E2) have reference to the same location on the heap, therefore when assigning E1 into E2 we set a pointer to the same location, and values change according.

Example 3 (String type):

The ‘String’ behavior as ‘Immutable’ object

As you can see the string variable is not changed, although it’s a reference type, the reason for this unique issue is the ‘Sting’ definition as ‘immutable’.

The String class

The string object is created from the “System. String” class, this class has many features that you can use while working with the string object, therefore I will try to cover the main functions that you can use to manipulate this object.

Creating String object

Option 1:

Creating a simple string object


Option 2:

Strings useful features:

Split your string

String contain array of chars/words, therefore we can manipulate it in both ways (Build and Split)

String Manipulations (Upper/Lowe)

The following example will demonstrate how to manipulate strings to Lowercase and Uppercase

Comparing Strings

Comparing strings can be made in few ways:

Locating inner string

Strings can be very large, for example string can host a full article with K of words, assuming we need to find a specific Word OR sentence, well it’s not a big issue, the following examples will demonstrate this issue.

String Copy
String copy allow us to replicate a string, we have 2 options to accomplish this issue:

Option 1:
String A = String B (String A will receive a reference to the B value on heap) 

Option 2:
Using the “String. Copy” Method, not like option 1 when using this method we actually create a new ‘String’ object that configured with the exact values of the source.

String Join

This method help us to combine multiple string into single one (the simple way is to overload the method with “separator type” and “Array location”), the Join method relevant to array of strings that we need to combine together, this method support simple array and generic list , I will show both examples

Remove chars from a string

This method is very simple, we can use it to remove specific chars in a string. When using it the result will returned as a new string.

 Replace strings

The replace method will help us to replace inner strings with new values.

To use this method we need to specify the value we want to changed (located on string) and the value we want to change into.
The string we need to replace must be overloaded as case sensitive.

Validating string with start/End word

This section will supply you the ability to validate you strings based on the first word or based on the last Word in any given string, the method return Boolean value (True OR False)
To achieve this issue we need to use 2 methods:
StartWith – Validate the first word in a string.
EndWith – validate the last word in a string.

Converting string into a cha array

Using this method will help us extracting every string into his basic elements (Array of chars).

This can be useful when we need to perform specific operations on chars that we cannot perform on strings. Remember that strings by definition are 'immutable' and therefore we cannot edit them after creation, therefore manipulating the char array is much friendlier when we need to edit specific chars.

Strings and Whitespaces

To handle strings spaces we can use the 'Trim' method, using this method will help us reducing some irrelevant spaces in a string.

Available methods:

TrimStart – Delete all spaces at the Start of the word.

TrimEnd - Delete all spaces at the End of the word.

Trim – combination of the first 2 methods.

String Length

Nothing special here, the 'Length' method will return the number of chars in a given string (including spaces).

No comments:

Post a Comment

My Presentations