How could i find string that it's the end with "," and "." ?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 21
Reputation: sotvisal has a little shameless behaviour in the past 
Solved Threads: 0
sotvisal sotvisal is offline Offline
Newbie Poster

How could i find string that it's the end with "," and "." ?

 
0
  #1
Jul 25th, 2009
import java.util.*;
class FindString
{
public static void main(String arg[])
{
Scanner ob=new Scanner(System.in);
String st="I am a student, you're also a .";
String search;
String st2="";
String st3="";
int i,c=0;
System.out.println(st);
System.out.println("Enter your word to search: ");
search=ob.next();
do
{
i=st.indexOf(search);
if(i!=-1)
{
st2=st.substring(0,i);
st2+=st3;
st2+=st.substring(i+search.length());
c=c+1;
st=st2;
}
}while(i!=-1);
System.out.println("Found=" + c);
}
}

Sorry for all friends, that i could use the bb tag code. Wish you all feel me .
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How could i find string that it's the end with "," and "." ?

 
0
  #2
Jul 25th, 2009
Originally Posted by sotvisal View Post
import java.util.*;
class FindString
{
public static void main(String arg[])
{
Scanner ob=new Scanner(System.in);
String st="I am a student, you're also a .";
String search;
String st2="";
String st3="";
int i,c=0;
System.out.println(st);
System.out.println("Enter your word to search: ");
search=ob.next();
do
{
i=st.indexOf(search);
if(i!=-1)
{
st2=st.substring(0,i);
st2+=st3;
st2+=st.substring(i+search.length());
c=c+1;
st=st2;
}
}while(i!=-1);
System.out.println("Found=" + c);
}
}

Sorry for all friends, that i could use the bb tag code. Wish you all feel me .
Java-specific BB Code.


[code=JAVA]
// paste code here
[/code]


Non-Java-specific BB Code.


[code]
// paste code here
[/code]


  1. class FindString
  2. {
  3. public static void main(String arg[])
  4. {
  5. Scanner ob=new Scanner(System.in);
  6. String st="I am a student, you're also a .";
  7. String search;
  8. String st2="";
  9. String st3="";
  10. int i,c=0;
  11. System.out.println(st);
  12. System.out.println("Enter your word to search: ");
  13. search=ob.next();
  14. do
  15. {
  16. i=st.indexOf(search);
  17. if(i!=-1)
  18. {
  19. st2=st.substring(0,i);
  20. st2+=st3;
  21. st2+=st.substring(i+search.length());
  22. c=c+1;
  23. st=st2;
  24. }
  25. }while(i!=-1);
  26. System.out.println("Found=" + c);
  27. }
  28. }

I don't understand what you're trying to do.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 41
Reputation: harsh2327 is an unknown quantity at this point 
Solved Threads: 7
harsh2327 harsh2327 is offline Offline
Light Poster

Re: How could i find string that it's the end with "," and "." ?

 
0
  #3
Jul 25th, 2009
Please explain your problem properly. Cant get it what you want.


From what I have understood... to find whether a string that ends with "," or ".",
eg.

  1. String str = "Hello World.";
  2. if(str.charAt(str.length-1)==',' || str.charAt(str.length-1)=='.')
  3. {
  4. // Your Code
  5. }
Last edited by harsh2327; Jul 25th, 2009 at 2:00 pm.
<(^.^)>.....HM.....<(^.^)>
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 21
Reputation: sotvisal has a little shameless behaviour in the past 
Solved Threads: 0
sotvisal sotvisal is offline Offline
Newbie Poster

Re: How could i find string that it's the end with "," and "." ?

 
0
  #4
Jul 26th, 2009
Hey friend from my willing is i want to find a string like
E.g. String str="Hello World.";
and let the user enter the word they wish to. Right after then if the user enter "World" i want my code return Found =1 and if they enter "World." it also the same. Wish you get it!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How could i find string that it's the end with "," and "." ?

 
1
  #5
Jul 26th, 2009
Originally Posted by harsh2327 View Post
Please explain your problem properly. Cant get it what you want.


From what I have understood... to find whether a string that ends with "," or ".",
eg.

  1. String str = "Hello World.";
  2. if(str.charAt(str.length-1)==',' || str.charAt(str.length-1)=='.')
  3. {
  4. // Your Code
  5. }
Huh? What's wrong with the "endsWith" method of String?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 41
Reputation: harsh2327 is an unknown quantity at this point 
Solved Threads: 7
harsh2327 harsh2327 is offline Offline
Light Poster

Re: How could i find string that it's the end with "," and "." ?

 
0
  #6
Jul 27th, 2009
Sorry for the late reply .. was away from a PC


you can endsWith() use that also ... no harm
Last edited by harsh2327; Jul 27th, 2009 at 6:12 am.
<(^.^)>.....HM.....<(^.^)>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 383 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC