【问题标题】:Trim last 4 characters of Object [closed]修剪对象的最后 4 个字符 [关闭]
【发布时间】:2012-05-16 00:22:39
【问题描述】:

我想修剪显示在第 3 个(Room Type) 和第 4 个(Meal type) 列中的对象的最后 4 个字符。因为在底部我提供了输出示例。您可以在第 3 和第 4 列中清楚地看到最后 4字符是括号中的价格,我想跳闸。

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 TypeMeal Type 的最后4 个字符绊倒?

【问题讨论】:

  • 您看过 Java API 吗?为String 定义了一个方法:subString(...)。我将把参数留给你自己弄清楚——阅读起来并不难。
  • @Thomas:我试过 substring() 但 RoomItem 和 MealItem 被声明为 Object 而不是字符串。所以当我尝试使用子字符串时,它显示“找不到符号”错误。
  • @Ravi 好吧,roomItemexact 类型是什么?它不能是Object,但必须是子类型。如果是s String` 那么,嗯……你听说过强制转换吗?
  • @Thomas:是的,我知道,但是当我将 roomItem 定义为字符串时,它没有存储我的结果并出现错误,然后我搜索它并得到了我需要定义为对象的结果。

标签: java string oop object substring


【解决方案1】:
String pricey = "Breakfast($10)";
String yummy = pricey.substring(0, pricey.length() - 4);

【讨论】:

  • 根据 Javadoc,最好看到 pricey.length() - 4 不会产生负值,因为这会导致异常。
  • @npinti - 好点。通用的字符串缩短方法应该进行检查或在方法文档中指定传递短于 4 个字符的字符串将引发异常。
  • 但是我的 roomItem 被声明为 Object 而不是 String。
【解决方案2】:

在发布此类问题之前,您应该先阅读 Java String API:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

然后,你可以使用类似 substring 的方法。

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.

【讨论】:

    【解决方案3】:

    你可以使用substring():

    String word = "Breakfast($10)".substring(0, "Breakfast($10)".length() - 4);
    

    【讨论】:

    • 根据 Javadoc,最好看到 pricey.length() - 4 不会产生负值,因为这会导致异常。
    • 当然,在尝试获取它的子字符串之前,您必须检查字符串的长度。
    • 不幸的是,此代码将返回最后四个字符,而不是最后四个字符。
    • 忘记了一个 0。很好,@TedHopp!
    • 但我的 roomItem 变量声明为 Object 而不是 String。 substring 和 length 是 String 函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 2015-04-24
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多