codepointat
大家好!今天让创意岭的小编来大家介绍下关于codepointat的问题,以下是小编对此问题的归纳整理,让我们一起来看看吧。
ChatGPT国内免费在线使用,一键生成原创文章、方案、文案、工作计划、工作报告、论文、代码、作文、做题和对话答疑等等
只需要输入关键词,就能返回你想要的内容,越精准,写出的就越详细,有微信小程序端、在线网页版、PC客户端
本文目录:
一、java字符串查找某个字符后面出现位置的方法!
用字符串里的indexOf方法,详情如下:
public int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。如果在此 String 对象表示的字符序列中出现值为 ch 的字符,则返回第一次出现该字符的索引(以 Unicode 代码单元表示)。对于 0 到 0xFFFF(包括 0 和 0xFFFF)范围内的 ch 的值,返回值是
this.charAt(k) == ch
为 true 的最小 k 值。对于其他 ch 值,返回值是
this.codePointAt(k) == ch
为 true 最小 k 值。无论哪种情况,如果此字符串中没有这样的字符,则返回 -1。
参数:
ch - 一个字符(Unicode 代码点)。
返回:
在此对象表示的字符序列中第一次出现该字符的索引;如果未出现该字符,则返回 -1。
二、string.indexof('_')返回-1表示什么
意思是donutString里是否含有"dozen"。
方法的作用是:返回字符串中指定字符串值的第一个匹配项。indexOf()方法是区分大小写的!如果要检索的字符串值没有出现,则该方法返回-1。
例如:
在“Helloworld!”不同的检索字符串:
<script type="text/javascript">
varstr="Helloworld!"
</script>
以上代码的输出:
0
-1
6
扩展资料:
用法
String.indexOf
PublicintindexOf返回指定字符首次出现在字符串中的索引。如果值为ch的字符出现在字符串对象表示的字符序列中,则返回该字符第一次出现的索引(以Unicode代码单位表示)。
对于0到0xFFFF范围内的ch的值(包括0和0xFFFF),返回值是其中的最小值k。charat(k)==ch是正确的。对于ch的其他值,返回值为。
这一点。CodePointAt(k)==ch是真k的最小值。在任何一种情况下,如果字符串中没有这样的字符,则返回-1。
参数:ch-一个字符(Unicode代码点)。
返回:在该对象表示的字符序列中第一次出现该字符的索引,如果未出现该字符,则返回-1。
三、java中string类的方法有哪些?
方法摘要
char charAt(int index)
返回指定索引处的 char 值。
int codePointAt(int index)
返回指定索引处的字符(Unicode 代码点)。
int codePointBefore(int index)
返回指定索引之前的字符(Unicode 代码点)。
int codePointCount(int beginIndex, int endIndex)
返回此 String 的指定文本范围中的 Unicode 代码点数。
int compareTo(String anotherString)
按字典顺序比较两个字符串。
int compareToIgnoreCase(String str)
不考虑大小写,按字典顺序比较两个字符串。
String concat(String str)
将指定字符串联到此字符串的结尾。
boolean contains(CharSequence s)
当且仅当此字符串包含 char 值的指定序列时,才返回 true。
boolean contentEquals(CharSequence cs)
当且仅当此 String 表示与指定序列相同的 char 值时,才返回 true。
boolean contentEquals(StringBuffer sb)
当且仅当此 String 表示与指定的 StringBuffer 相同的字符序列时,才返回 true。
static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的字符串。
static String copyValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的字符串。
boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结束。
boolean equals(Object anObject)
比较此字符串与指定的对象。
boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 进行比较,不考虑大小写。
static String format(Locale l, String format, Object... args)
使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
static String format(String format, Object... args)
使用指定的格式字符串和参数返回一个格式化字符串。
byte[] getBytes()
使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过 getBytes() 构造方法,该方法使用平台的默认字符集。
byte[] getBytes(String charsetName)
使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。
int hashCode()
返回此字符串的哈希码。
int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
int indexOf(int ch, int fromIndex)
从指定的索引开始搜索,返回在此字符串中第一次出现指定字符处的索引。
int indexOf(String str)
返回第一次出现的指定子字符串在此字符串中的索引。
int indexOf(String str, int fromIndex)
从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
String intern()
返回字符串对象的规范化表示形式。
int lastIndexOf(int ch)
返回最后一次出现的指定字符在此字符串中的索引。
int lastIndexOf(int ch, int fromIndex)
从指定的索引处开始进行后向搜索,返回最后一次出现的指定字符在此字符串中的索引。
int lastIndexOf(String str)
返回在此字符串中最右边出现的指定子字符串的索引。
int lastIndexOf(String str, int fromIndex)
从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
int length()
返回此字符串的长度。
boolean matches(String regex)
通知此字符串是否匹配给定的正则表达式。
int offsetByCodePoints(int index, int codePointOffset)
返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
boolean regionMatches(int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。
String replace(CharSequence target, CharSequence replacement)
使用指定的字面值替换序列替换此字符串匹配字面值目标序列的每个子字符串。
String replaceAll(String regex, String replacement)
使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的每个子字符串。
String replaceFirst(String regex, String replacement)
使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的第一个子字符串。
String[] split(String regex)
根据给定的正则表达式的匹配来拆分此字符串。
String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串。
boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。
boolean startsWith(String prefix, int toffset)
测试此字符串是否以指定前缀开始,该前缀以指定索引开始。
CharSequence subSequence(int beginIndex, int endIndex)
返回一个新的字符序列,它是此序列的一个子序列。
String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。
String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
char[] toCharArray()
将此字符串转换为一个新的字符数组。
String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
String toLowerCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
String toString()
返回此对象本身(它已经是一个字符串!)。
String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
String toUpperCase(Locale locale)
使用给定的 Locale 规则将此 String 中的所有字符都转换为大写。
String trim()
返回字符串的副本,忽略前导空白和尾部空白。
static String valueOf(boolean b)
返回 boolean 参数的字符串表示形式。
static String valueOf(char c)
返回 char 参数的字符串表示形式。
static String valueOf(char[] data)
返回 char 数组参数的字符串表示形式。
static String valueOf(char[] data, int offset, int count)
返回 char 数组参数的特定子数组的字符串表示形式。
static String valueOf(double d)
返回 double 参数的字符串表示形式。
static String valueOf(float f)
返回 float 参数的字符串表示形式。
static String valueOf(int i)
返回 int 参数的字符串表示形式。
static String valueOf(long l)
返回 long 参数的字符串表示形式。
static String valueOf(Object obj)
返回 Object 参数的字符串表示形式。
从类 java.lang.Object 继承的方法
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
四、Java怎么判断判断某个字段中汉字的长度在2到20内
代码如下:
/**
* 在Java中按字节获得字符串长度的两种方法
*
*/
public class App
{
public static void main( String[] args ) throws UnsupportedEncodingException
{
String str = "只";
System.out.println(getWordCount(str));
System.out.println(getWordCountRegex(str));
System.out.println(getWordCountCode(str,"GBK"));
System.out.println(getWordCountCode(str,"UTF-8"));
}
/*由于Java是基于Unicode编码的,因此,一个汉字的长度为1,而不是2。
* 但有时需要以字节单位获得字符串的长度。例如,“123abc长城”按字节长度计算是10,而按Unicode计算长度是8。
* 为了获得10,需要从头扫描根据字符的Ascii来获得具体的长度。如果是标准的字符,Ascii的范围是0至255,如果是汉字或其他全角字符,Ascii会大于255。
* 因此,可以编写如下的方法来获得以字节为单位的字符串长度。*/
public static int getWordCount(String s)
{
int length = 0;
for(int i = 0; i < s.length(); i++)
{
int ascii = Character.codePointAt(s, i);
if(ascii >= 0 && ascii <=255)
length++;
else
length += 2;
}
return length;
}
/*基本原理是将字符串中所有的非标准字符(双字节字符)替换成两个标准字符(**,或其他的也可以)。这样就可以直接例用length方法获得字符串的字节长度了*/
public static int getWordCountRegex(String s)
{
s = s.replaceAll("[^\\x00-\\xff]", "**");
int length = s.length();
return length;
}
/*按特定的编码格式获取长度*/
public static int getWordCountCode(String str, String code) throws UnsupportedEncodingException{
return str.getBytes(code).length;
}
}
以上就是关于codepointat相关问题的回答。希望能帮到你,如有更多相关问题,您也可以联系我们的客服进行咨询,客服也会为您讲解更多精彩的知识和内容。
推荐阅读: