Strings : Java and C#

In this article, I would be discussing with you the concept of strings.. and what does modern programming languages like Java and C# have in store for String manipulation.

Let's first start with understanding “what is a String".

A string is basically a sequence or a string of Unicode characters. It is one of the most common data types that a programmer deals with. Input validation, text analysis and file conversions are several direct applications of string manipulation.

In some languages such as C, strings are treated as character arrays. Whereas, Java and C#, works with complete strings and treat them as objects.

Let’s now move on to discuss the implementation of strings in java and C#.

Strings in Java :

For string manipulation in java, in java.lang package we have a class called String which extends Object class. String objects are created so as to store character sequences. It may contain null as well.

String Class is immutable, i.e. Strings in java, once created and initialized, cannot be changed on the same reference. A java.lang.String class is final which implies no class can extend it. The java.lang.String class differs from other classes. One difference being that the String objects can be used with the += and + operators for concatenation.

A simple String can be created using a string literal enclosed inside double quotes as shown :

String str = "Tech Yoddha";

Or by passing some characters to the constructor like this :

String str = new String("Tech Toddha");

Since a string literal is a reference, it can be manipulated like any other String reference. The reference value of a string literal can be assigned to another String reference.

If 2 or more Strings have the same set of characters in the same sequence then they share the same reference in memory. Such strings are said to be interned, meaning that they share a unique String object. The String class maintains a private pool where such strings are interned.

I will be discussing more on String Interning in my upcoming article. As of now, lets continue with String creation..

Constructing String objects can also be done from arrays of bytes, arrays of characters, or string buffers. A simple way to convert any primitive value to its string representation is by concatenating it with the empty string (""), using the string concatenation operator (+).

String class contains many useful methods for string manipulation. Lets see some of the them :

boolean equals(Object obj) and boolean equalsIgnoreCase(String str) : The comparison using equals() is case-sensitive. To perform a comparison that ignores case differences, call equalsIgnoreCase( ).

Example:

String s1 = "Hello";
String s2 = "Hello";
String s3 = "Good-bye";
String s4 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> “+s1.equalsIgnoreCase(s4));

• int length() : This function returns an int which is the number of characters present in a String variable.

Example:

String str = "Tech yoddha";
System.out.println("Length : "+str.length);
//output : 11

• int compareTo(String str) and int compareToIgnoreCase(String str) : This method is used to compare two strings.
It returns 0 if both strings are same,
It returns a negative value if first string is less than the second string and
It returns a positive value if first string is greater than the second string.

Example:

String smaller = "abc";
String greater = "bcd";
If(greater.compareTo(smaller) >0)
System.out.println(greater+" is greater than "+smaller);
else
System.out.println(greater+" is lesser than "+smaller);
// output: bcd is greater than abc

• String replace(char oldChar, char newChar) : This method is used to replace a character with another character throughout the string.

Example:


String s1 = "java" , s2 ;
s2=s1.replace('a','#');
Now a in java is replaced by #. So s2 is referring to j#v#.

• boolean startsWith(String prefix) and boolean endsWith(String suffix) : These two methods are used to check respectively whether the string starts with or ends with a particular character combination.

• String trim() : This method is used to remove leading and trailing spaces from a string object.

There are lot more functions in String class for string manipulation like indexOf(), split(), substring(), toUpperCase(), toLowerCase(), charAt() etc.

You can also use StringBuilder and StringBuffer classes for manipulating strings.

Strings in C# :


C# or Java, the basic concept of string remains the same.

Like Java, in C# also, the string type represents a string of Unicode characters. Here, we have System.String class which we can use for string manipulation.

Lets discuss over String class in C# now.

But before that, I want to tell you something about string and String in C#. Many people think that string with a small 's' is different than String with capital 'S'. But actually string is an alias for System.String in the .NET Framework. That means string is same as String.
So, keep in mind that whether you write string or String, to C# compiler both means the same.

Lets continue with class String now..

The methods and features of System.String is similar to java.lang.String, but there are a few different things here.

In C#, Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references. This makes testing for string equality more intuitive.

The + operator concatenates strings like this :

string a = "good " + "morning";

The [] operator accesses individual characters of a string:

char x = "test"[2]; // x = 's';

String literals are of type string and can be written in two forms, quoted and @-quoted.

Quoted string literals are enclosed in double quotation marks ("):
"good morning" // a string literal

@-quoted string literals start with @ and are enclosed in double quotation marks. For example:
@"good morning" // a string literal

The advantage of @-quoting is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:
@"c:\Docs\Source\a.txt"
// rather than "c:\\Docs\\Source\\a.txt"

To include a double quotation mark in an @-quoted string, double it:
@"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.

You may try this example :

// keyword_string.cs
using System;
class test
{
public static void Main( String[] args )
{
string a = "\u0068ello "; // \u0068 is the unicode
representation for letter “h”.

string b = "world";
Console.WriteLine( a + b );
Console.WriteLine( a + b == "hello world" );
}
}

Rest all the functions are more or less same as that in java.lang.String class.

Thus, we learnt the basic concept of Strings.. and how they are implemented in C# and java.. and also discussed a few places where C# String class varies from java String class.

References :
  • http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
  • http://msdn.microsoft.com/en-us/library/362314fe.aspx
  • http://www.java2s.com/Tutorial/Java/0040__Data-Type/equalsandequalsIgnoreCase.htm

1 comment:

Ritesh Raghuvanshi said...

Sindhu, you have done a fantastic job by authoring this article.

You have explained it so beautifully, that nobody can dare to not understand this.

The article has a very nice flow of explanation.

Great !!

Keep on writing and informing your readers like me.