0

how to remove comma from last of three string..

my program output is USA,Dubai,India, what i want-USA,Dubai,India

28th Nov 2016, 10:51 AM
Gurpreet Ganger
Gurpreet Ganger - avatar
17 odpowiedzi
+ 2
If always last character is , then you can use this, str = str.substring(0, str.length - 2); Hope this will help you
28th Nov 2016, 10:58 AM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 2
You should place this outside the for loop, like this for (string v1.....) { .... } data = data.substring (0,data.length - 2); Hope you understand this time
28th Nov 2016, 11:16 AM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 2
Sorry, length is not the member variable. It's a method so call like data = data.substring(0, data.length() - 1); For you reference I have added the sample in my code playground something like "Substring to remove last character"
28th Nov 2016, 11:42 AM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 2
Check my code in playground and it's working it may help you
28th Nov 2016, 11:52 AM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 2
Put substring statement outside the loop Based on the op I guess u put this statement inside the loop
28th Nov 2016, 12:25 PM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 2
Anytime brother ☺
28th Nov 2016, 12:27 PM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 1
Did you try with substring approach?
28th Nov 2016, 11:11 AM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 1
Guess it's working now
28th Nov 2016, 12:21 PM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
0
import java.awt.*; import java.awt.event.*; import java.awt.List; class samplem extends Frame implements ActionListener { Label l1; Button b1; List l; samplem() { setLayout(null); l1 =new Label (); b1 =new Button("Submit"); l=new List(3,true); l.add("India"); l.add("USA"); l.add("China"); l.add("Japan"); l.add("Netherlands")­; l1.setBounds(50,300,­300,20); b1.setBounds(50,200,­100,20); l.setBounds(50,100,1­00,50); b1.addActionListener­(this); add(b1); add(l); add(l1); } public void actionPerformed(Acti­onEvent aa) { if (aa.getSource()==b1)­; { String data ="Selected Countries are : "; //Variable for(String v:l.getSelectedItems­()) { data=data + v+","; v.deleteCharAt(v.len­gth() - 1); } l1.setText(data); } } public static void main(String a[]) { samplem ob=new samplem(); ob.setSize(500,500); ob.setVisible(true); } }
28th Nov 2016, 11:08 AM
Gurpreet Ganger
Gurpreet Ganger - avatar
0
i triedthis v.deleteCharAt(v.lenth()-1);
28th Nov 2016, 11:10 AM
Gurpreet Ganger
Gurpreet Ganger - avatar
0
it did not work..
28th Nov 2016, 11:10 AM
Gurpreet Ganger
Gurpreet Ganger - avatar
0
can u please implement it in the coding..it will help me alot to understand
28th Nov 2016, 11:13 AM
Gurpreet Ganger
Gurpreet Ganger - avatar
0
error is. data=data.substring(0,data.length-2) ^ cant fine symbol
28th Nov 2016, 11:27 AM
Gurpreet Ganger
Gurpreet Ganger - avatar
0
in implemented like... for(String...... { data=data.... } data=data.substring(0...... l1.setText(data)
28th Nov 2016, 11:29 AM
Gurpreet Ganger
Gurpreet Ganger - avatar
0
same error dear :( ..
28th Nov 2016, 11:49 AM
Gurpreet Ganger
Gurpreet Ganger - avatar
0
works but removed all between the countries.. result showing.. IndiaUsaChina
28th Nov 2016, 12:23 PM
Gurpreet Ganger
Gurpreet Ganger - avatar
0
it works bro..i was implementing it in for loop.. now implemented it out side loop..its working ... thanks my dear brother you helped me alot :) God bkess you
28th Nov 2016, 12:26 PM
Gurpreet Ganger
Gurpreet Ganger - avatar