• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help on String methods!!!

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

I am getting confused with the working of String class
methods such as
substring()
replace()
split()

please if anyone could explain me the working
of this methods in a easy to understand language
with an example ,it will be kinda of you!!!


Thanks in advance!!!
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dhwani,
I think this will help you .

public class StringMethod {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="my java certification";
System.out.println(s.substring(4));// Displays the string from index 4 to end of the string
System.out.println(s.substring(3, 7)); //Displays the string from index 3 to index 6 (before index 7)
System.out.println(s.replace('a', 'b'));//Where ever a is present it changes to b
String s1[]=s.split("\\s"); // where there is space split the string
for(int i=0;i<s1.length;i++)
System.out.println(s1[i]);
}
}
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sarada chellu thanks for your help !!!


This example is realy interesting ,
but still have some doubts if you could clear




i understood the replace method,

but i am in doubt with split methods output............

it will be kinder if you ellaborate and explain these concepts.........


Thanks in advance
 
sarada chellu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before answering your question
I just want to tell that string starts from index 0 so if length=1 then index is 0 same like arrays.

1)so here it displays from index 4 so does it consider space?
a)yes ,it consider space while counting.

2)so here it displays jav ?please correct me if i am wrong........
a) This is wrong ! it dispalys java.

3)About split?
a) The string will be spliting in the way you mentioned inside the parenthesis. It takes regex(Regular Expression for more info see java.util.regex) as parameter ,in the above example '\s' indicates space .you can mention in your on way eg :something like split where ever 'a' is presented in the string (similar to stringTokenizer)
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dhwani mathur:

[CODE]String s="my java certification";
System.out.println(s.substring(4));// Displays the string from index 4 to end of the string

so here it displays from index 4 so does it consider space?
or it will display from letter v which is fourth index of string
?



Actually index 4 is the first 'a' in 'java'. Look at the string and count the characters, starting with index 0, and of course include the spaces in the counting.

One more thing you should know about these methods is that, unlike other languages, the string object itself is not changed, even with replace(). The result is only returned as a separate String. String objects are immutable.
 
sarada chellu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi greg,
Since the string is "my java certification"
Here Actually index 4 is the first 'a' in my ja which count's from m i.e. index 0 and stops at a i.e index 4 but not java
I think it's clear
 
sarada chellu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOps! sorry greg,
What you mentioned is right !
I though you are counting from java .
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dhwani wrote:


I am getting confused with the working of String class
methods such as
substring()
replace()
split()

please if anyone could explain me the working
of this methods in a easy to understand language


OK, I take Java as the easy to understand language



All three of these methods are overloaded, you should study the API and do some experiments for yourself.


Yours,
Bu.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
sarada chellu &
Greg Buela

for such a gud explanation
i am clear with my concepts!!!

Thanks to Burkhard Hassel for this example
i am working on this example,its realy
interesting one!!!
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope you liked the .
Bu.


(Only one sentence, and I still needed to edit. Thank god it's friday.=
[ September 28, 2007: Message edited by: Burkhard Hassel ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic