0
how to remove comma from last of three string..
my program output is USA,Dubai,India, what i want-USA,Dubai,India
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
+ 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
+ 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"
+ 2
Check my code in playground and it's working it may help you
+ 2
Put substring statement outside the loop
Based on the op I guess u put this statement inside the loop
+ 2
Anytime brother ☺
+ 1
Did you try with substring approach?
+ 1
Guess it's working now
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,100,50);
b1.addActionListener(this);
add(b1);
add(l);
add(l1);
}
public void actionPerformed(ActionEvent aa)
{
if (aa.getSource()==b1);
{
String data ="Selected Countries are : "; //Variable
for(String v:l.getSelectedItems())
{
data=data + v+",";
v.deleteCharAt(v.length() - 1);
}
l1.setText(data);
}
}
public static void main(String a[])
{
samplem ob=new samplem();
ob.setSize(500,500);
ob.setVisible(true);
}
}
0
i triedthis v.deleteCharAt(v.lenth()-1);
0
it did not work..
0
can u please implement it in the coding..it will help me alot to understand
0
error is. data=data.substring(0,data.length-2)
^
cant fine symbol
0
in implemented like... for(String......
{
data=data....
}
data=data.substring(0......
l1.setText(data)
0
same error dear :( ..
0
works but removed all between the countries.. result showing.. IndiaUsaChina
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