-10

3 列目と 4 列目に表示されているオブジェクトの最後の 4 文字をトリミングしたい(Room Type)(Meal type)列の下にあるように、出力のサンプルを提供しました。トリップする。

public void showAll()

    {
        String name ="";
        String ID="";
        Object roomItem;
        Object mealItem;
        int roomIn;
        int meal;
        int days=0;
        double tprice=0;

        display.setText("");
        display.append("ID  Customer Name   RoomType    MealType    Days    TotalCharge($)");
        display.append("\n ---------------------------------");

        for (int i = 0; i < myList.size(); i++)
           {
        Customer c = myList.get(i);

        ID = c.getID();
        name = c.getName();
        roomIn = c.getRoomIndex();                  // Get the room index stored in Linked list
        roomItem = roomTypeCombo.getItemAt(roomIn); // Get the item stored on that index.
        meal = c.getMealIndex();                    // Get the Meal index stored in Linked list
        mealItem = mealCombo.getItemAt(meal);       // Get the item stored on that index.
        days = c.getDaysIndex();
        tprice = c.getTotalPrice();
        display.append("\n"+ID+"    "+name+"        "+roomItem+"    "+mealItem+"    "+days+ "   "+tprice);
            }
        display.append("\n \n Total "+myList.size()+" Entrie(s) !");

    } // end of function

そして、私のプログラムの出力は次のようになります:

ID  Customer Name           RoomType    MealType    Days    TotalCharge
__________________________________________________________________

234 John Andersen       Standard($75)   Any Two($30)     4    420.0

Room Typeとの最後の 4 文字を削除するにはどうすればよいMeal Typeですか?

4

3 に答える 3

2
String pricey = "Breakfast($10)";
String yummy = pricey.substring(0, pricey.length() - 4);
于 2012-05-07T13:34:52.930 に答える
1

この種の質問を投稿する前に、まず Java String API: http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlを読む必要があります。

次に、部分文字列メソッドのようなものを使用できます。

public String substring(int beginIndex,
               int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

     "hamburger".substring(4, 8) returns "urge"
     "smiles".substring(1, 5) returns "mile"


Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive.
Returns:
    the specified substring.
Throws:
    IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
于 2012-05-07T13:36:59.540 に答える
0

使用できますsubstring()

String word = "Breakfast($10)".substring(0, "Breakfast($10)".length() - 4);
于 2012-05-07T13:34:13.003 に答える