0

Possible Duplicate:
java - removing semi colon from a string if the string ends with it

"Dear $contact_name$,

Your job posting for $job_title$ has been submitted to the following job boards as Free Job Boards $Free_Job_Boards$, $paid_job_boards$. Each job board can take up to 48 hours to process the request.

Thank you,

Here i want to remove comma after $Free_Job_Boards$ when there is no paid jobs. when i use content=content.replaceAll(" ,","") then comma after $contact_name$ also removed

Community
  • 1
  • 1
Jisha Vijayan
  • 19
  • 1
  • 3
  • Look at the API documentation of `java.lang.String` and see if there are any methods in there that you can use for this. – Jesper Oct 12 '12 at 06:55

4 Answers4

8
String s = "dsd,fsf,fsfsfsf,rwrw.com,ryriyry,hfhfhfh,";
if (s.endsWith(",")) {
    s = s.substring(0, s.length() - 1);
}
pap
  • 27,064
  • 6
  • 41
  • 46
2
    String str=" dsd,fsf,fsfsfsf,rwrw.com,ryriyry,hfhfhfh,";
    if(freeJob && str.endsWith(","))
        str=str.substring(0,str.lastIndexOf(","));
    System.out.println(str);
amicngh
  • 7,831
  • 3
  • 35
  • 54
  • Thanks for your help. But actually str is dsd,fsf,fsfsfsf,rwrw.com,ryriyry,hfhfhfh and last comma is used for adding paid job contents. But when there is only free job board,this comma is not needed. but it exist – Jisha Vijayan Oct 12 '12 at 06:58
  • then you can check availability of "," at the end of string and then replace it . like pap has posted. – amicngh Oct 12 '12 at 07:01
2

You can also use:

StringBuilder b = new StringBuilder(yourString);
b.replace(yourString.lastIndexOf(","), yourString.lastIndexOf(",") + 1, "" );
yourString = b.toString();
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
1

Extract the sub string, from index 0 to the last index of ,: -

String s = "whatever your String"; 
String s = s.substring(0, s.lastIndexOf(','));
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Pr38y
  • 1,565
  • 13
  • 21
  • "Dear $contact_name$, Your job posting for $job_title$ has been submitted to the following job boards as Free Job Boards $Free_Job_Boards$, $paid_job_boards$. Each job board can take up to 48 hours to process the request. Thank you, Here i want to remove comma after $Free_Job_Boards$ when there is no paid jobs. – Jisha Vijayan Oct 12 '12 at 07:33
  • That was a hint, for your specific scenario you can add a condition `if($paid_job_boards==null)`. – Pr38y Oct 12 '12 at 07:47