In: Computer Science
1.In C++, C#, Java. Discuss string operations functions, with examples. Then make Comparison of programming languages
2.String and StringBuffer (C#, Java) with examples.
In C++, string is an object of std::string class that represents sequence of characters. We can perform many operations on strings such as concatenation, comparison, conversion etc.
int compare(const string& str) | It is used to compare two string objects. |
int length() | It is used to find the length of the string. |
void swap(string& str) | It is used to swap the values of two string objects. |
string substr(int pos,int n) | It creates a new string object of n characters. |
In C# programming, string
is another kind of data
type that represents Unicode Characters. It is the alias of
System.String, however, you can also write System.String instead of
a string. It is the sequence of character in which each character
is a Unicode character.
String Functions | Definitions |
---|---|
Clone() |
Make clone of string. |
CompareTo() |
Compare two strings and returns integer value as output. It returns 0 for true and 1 for false. |
Contains() |
The C# Contains method checks whether specified character or string is exists or not in the string value. |
EndsWith() |
This EndsWith Method checks whether specified character is the last character of string or not. |
In Java, a string is a sequence of characters. For example,
"hello" is a string containing a sequence of characters 'h', 'e',
'l', 'l', and 'o'.Unlike other programming languages, strings in
Java are not primitive types (like int
,
char
, etc). Instead, all strings are objects of a
predefined class named String
Methods | Description |
---|---|
concat() |
joins the two strings together |
equals() |
compares the value of two strings |
charAt() |
returns the character present in the specified location |
getBytes() |
converts the string to an array of bytes |
indexOf() |
returns the position of the specified character in the string |
C# | C++ |
---|---|
Comparably slower. | Comparably faster. |
Mainly built for Windows platform. | Compatible with multiple platforms including Windows, Linux and Mac. |
Used for Web or Desktop application. | Mainly used for Performance-oriented application with hardware interaction. |
Has inbuilt garbage collector. | No support for garbage collection. |
Compiler warnings are displayed at the time of writing the code. | Need to write and compile the entire code to check for error. |
C# | Java |
---|---|
C# runs on CLR. | Java runs on JVM. |
C# needs .Net framework to run. | JDK is required for Java. |
C# can be used to develop both Web, and Game development along with Mobile development. | Java is mainly used for designing complex web applications. |
Not as efficient as Java. | Very efficient, flexible and cross platform compatible. |
Offers single type of exception. | Offers both check and uncheck exceptions. |
Libraries development and update depends upon Microsoft. | Open source nature allows continuous development and update. |
Decent server side performance. | Useful for server side interaction. |
Parameter of Comparison | C++ | Java |
---|---|---|
Based on the concept of | Write once compile anywhere | Write once run anywhere everywhere |
Type of Programming Language | Procedural and object-oriented language | Only object-oriented language |
Type of Language | Compiled | Compiled + Interpreted |
Dependency on platform | Platform dependent language | Not dependent on platform |
Other languages compatibility | Yes, with most high-level languages | Not compatible, no backward compatibility |
Mechanism of Input | Input/output statements | More complex |
Relationship of source code and filename | No relationship | Yes, relationship exists |
Interface with libraries | Allows direct calls to native system libraries | Only through Java native interface |
Portability | Not portable | Portability is there as it can be executed on any platform |
Is operator overloading allowed? | Yes | No, only method overloading allowed |
Type of root hierarchy | No root hierarchy | Follows single root hierarchy |
Access control | Flexible | Complex |
Detection of runtime error responsibility | Responsibility of programmer | System controlled |
Management of Memory | Manual | System-managed |
2)In c#
No | String | StringBuffer |
1 | String class is immutable | StringBuffer class is mutable (modifiable) |
2 | Consumes more memory during concatenation | Consumes less memory during concatenation |
3 | Slow performance | Fast performance |
4 | Overrides equals() and hashcode() methods of the object class | Doesn't override the equals() and hashcode() methods of the object class |
In java
No. | String | StringBuffer |
---|---|---|
1) | String class is immutable. | StringBuffer class is mutable. |
2) | String is slow and consumes more memory when you concat too many strings because every time it creates new instance. | StringBuffer is fast and consumes less memory when you cancat strings. |
3) | String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method. | StringBuffer class doesn't override the equals() method of Object class. |
***********************************************************************************************************************************
In case of any doubt do ask in the comment section