Thursday, March 18, 2010

Built-in-Functions (String) – Enjoy it!

Let us plunge to another episode of functions, I know it is boring; at the same time it is most handy when you want to finish a program with lots of mathematical formula. Last week I have gone through a few numerical functions and the way functions help us for converting one data type to other. Today mainly I will discuss about string function, which only valid to string type data. String handling is not an easy job and you will find many functions available for this, but often while you write a program, you will be asked to write your own functions only and prohibited from using some of the string function. So, how to do this I will come to this when we talk about programming. String functions in Java return value is string but some return integer also. In java java.lang is default package so you do not need any extra package to be import but in C/C++ you require "string.h".

Note : Str - is the String argument you have to pass and Num is a Number of character and pos is the position.

DescriptionBasicC/C++Java
Find the length of a string value
len(Str$)

strlen(Str)

Str.length()
Extract a character from the said string from given
position

mid$(Str$,pos,num)

str(pos)

Str.charAt(pos)
Check equality between two string and return a
Boolean value/

Str1.equals(Stri2)
Check equality between two string ignoring he case
difference and return a Boolean value.

Str1.equalsIgnorecase(Str2)
Compare between two string( relevant to the ASCII
values of each character), if both equal it return 0, if String1 is
smaller than String2 then it will return -1 and if String1 is bigger
than String2 then it will return 1.

strcmp(Str1,Str2)
strcmpi(Str1,Str2)
(strcmpi() - for ignoring
case)

Str1.compareTo(Str2)
Extract all the string from given position till end.

String.substring(int)
Extract all the string from starting position up to
left of end position.

mid$(Str$,pos,num)
left$(Str$,num)
right$(Str$,num)
Using any loop from 0 position till
length of the string, we can extract character by character
Str1.substring(int,int) -
Join String2 to the right of String1Str1$+Str2$strcat(Str1,Str2)
Str1.concat(Str1)
This can be done by:
String1=String1+ String2.
Returns the index within this string of the first
occurrence of the specified character.

Str.indexOf(char/string)
Returns the index within this string of the first
occurrence of the specified character, starting the search at the
specified index.

Str.indexOf(char/string,
int)
Returns the index within this string of the last
occurrence of the specified character.

Str.lastIndexof(char/String)
Tests if this string starts with the specified
prefix.

Str.startswith(String)
Tests if this string ends with the specified suffix.
Str.endsWith(String)
Removes white space from both ends of this string.
Str.trim()
Returns a new string resulting from replacing all
occurrences of oldChar in this string with newChar.

Str.replace(char, char)
Str.replace(Str1, Str2)
Converts this String to lowercase / Converts this
String to upper case.
strlwr(Str) / strupr(Str)Str.toLowerCase() / Str.toUpperCase()
Converts this string to a new character array.Str.toCharArray()
In java we also having StringBuffer, so giving you some idea of this class-

A string buffer implements a changeable sequence of characters. String buffers are safe for use by multiple threads. The methods are
synchronized where necessary so that all the operations on any particular
instance behave as if they occur in some serial order. String buffers are used by the compiler to
implement the binary string concatenation operator +. For example, the code:

x = "a" + "4" +"c";

is compiled to the equivalent of:

x = new StringBuffer().append("a").append(4).append("c").toString()

Functions under StringBuffer
DescriptionJava
Constructs a string buffer with no
characters in it and an initial capacity specified by the length
argument.

StringBuffer()

Constructs a string buffer with no
characters in it and an initial capacity of 16 characters.

StringBuffer(int)
Constructs a string buffer so that it
represents the same sequence of characters as the string argument.

StringBuffer(String)
Appends the string representation of the
boolean argument to the string buffer.

append(boolean)
Appends the string representation of the
char argument to this string buffer.

append(char)
Appends the string representation of the
char array argument to this string buffer.

append(char[])
Appends the string representation of a
subarray of the char array argument to this string buffer.
append(char[], int, int)
Appends the string representation of the
int, float or double argument to this string buffer.

append(int) /append(float) /append(double)
Returns the current capacity of the
String buffer.

capacity()
Returns the character at a specific
index in this string buffer.

charAt(int)
Inserts the string representation of the
char argument into this string buffer.

insert(int, char) **
Inserts the string representation of the
char array argument into this string buffer.

insert(int, char[])
Returns the length (character count) of
this string buffer.

length()
Characters are copied from this string
buffer into the destination character array.

getChars(int, int, char[], int)
Sets the length of this String buffer.
setLength(int)
The character at the specified index of
this string buffer is set to ch.
setCharAt(int, char)
The character sequence contained in this string
buffer is replaced by the reverse of the sequence.
reverse()
Converts to a string representing the data in this
string buffer.
toString()
** Insert can be used as append for numeric too.

Here is a example, how we can use string buffer
System.out.println("StringBuffer insert and append example!");

StringBuffer sb = new StringBuffer(0);

System.out.println(sb.insert(0, "vinod"));

int len = sb.length();

System.out.println(sb.insert(len, "Deepak"));

System.out.println(sb.insert(6, "Raj"));

System.out.println(sb.append("Mohit"));



Database packages also provide some date and database functions, while writing about database I certainly cover those functions. I also remind you that if you feel anything left out then can suggest me, your suggestion will be always welcome.
!!!I know most of the students are busy with their respective examination and hardly have time to join this function, so Aburz for all.!!!

No comments: