subString.java

// subString.java
// to demonstrate to find a sub string
// to run the program C>java SubString
import java.io.*; // for I/O
//////////////////////////////////////////////////////////
// How to resolve the problem ? 
// =>Find the array that contain all the character of the sub string.
// Connect the character from the position ‘start’ to the ‘end’, we have the sub //string.
//////////////////////////////////////////////////////////
class SubString
{
public static String subString(String s, int start, int end)
{
int i = 0;
char subarray[] = new char[end - start + 1];
// get all the characters in sub string into an array
for(i = 0;i <= (end – start);i++)
{
subarray[i] =s.charAt(start+i);
}
// create sub string from the new array.
String substr = new String(subarray);
return substr;
}
     //
public static String subString2(String s, int start, int end)
{
int i;
String substr = “”;
   // get 1 character in sub string into an array that only has one element.
for(i = start;i <= end;i++)
{
char subarray[] = new char[1];
subarray[0] = s.charAt(i);
String subsubstr = new String(subarray);
 // Create sub string by use the method concat
substr = substr.concat(subsubstr);
}
return substr;
}
public static void main(String[] args)
{
System.out.println(subString2(“0123456789″,1,3));
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s