Java中那些熟悉又模糊的下标

宽为限 紧用功 功夫到 滞塞通

开篇

开发时是不是偶尔会遇到下标越界的问题,哈哈~ 索引有些从0开始,有些从1开始,不认真记一下还真容易混淆。+_+

sql

Hibernate占位符?和:及JPA,hibernate占jpa

小结一下hibernate占位符.

1.最常见的?占位符.

String hql = "select a from Apple a where a.color=? a.weight>?";
Query query = session.createQuery(hql);
query.setParameter(0, "red");
query.setParameter(1, "10");

下标从0开始,最常见的.这个让人头疼的是数?个数…

2.以一个变量名的形式占位.

String hql = "select a from Apple a where a.color=:pcolor a.weight>:pweight";
Query query = session.createQuery(hql);
query.setParameter("pcolor", "red");
query.setParameter("pweight", "10");

这个就不存在数?个数的问题了.应该是比较方便的一种方法了

3.JPA方式,这种方式是1的改良版本..

String hql = "select a from Apple a where a.color=?2 a.weight>?5";
Query query = session.createQuery(hql);
query.setParameter("2", "red");
query.setParameter("5", "10");

方法1中的?的索引可以自己随意任命了..

substring

以下是Java官方API对substring 的描述:

public String substring(int beginIndex)

Returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

返回一个字符串,该字符串是此字符串的子字符串。
子字符串以指定索引处的字符开始,并延伸到该字符串的末尾。

Examples:

“unhappy”.substring(2) returns “happy”
“Harbison”.substring(3) returns “bison”
“emptiness”.substring(9) returns “” (an empty string)

Parameters:
beginIndex - the beginning index, inclusive. 开始指数,包含。

Returns:
the specified substring. 指定的子字符串。

Throws:
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.

IndexOutOfBoundsException - 如果beginIndex为负或大于此String对象的长度。

public String substring(int beginIndex, int endIndex)

Returns a 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.

返回一个字符串,该字符串是此字符串的子字符串。子字符串从指定的beginIndex开始,并扩展到索引endIndex - 1(下标从0开始)处的字符。因此,子字符串的长度为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.

IndexOutOfBoundsException - 如果beginIndex为负数,或endIndex大于此String对象的长度,或beginIndex大于endIndex。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值