「java判断字符串格式」java判断字符串格式大小
本篇文章给大家谈谈java判断字符串格式,以及java判断字符串格式大小对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、在java字符串中如何判断字符的类型
- 2、java 如何判断字符串编码格式
- 3、java怎么判断输入的字符串是否为时间格式
- 4、技术问题:java中如何判断字符串内容是否是一种编码格式
- 5、怎么 知道 java字符串 编码格式
在java字符串中如何判断字符的类型
public static void main(String[] args) {
String str="ABC_001";
if(str.indexOf("ABC")!=-1){
System.out.println("包含");
}else{ System.out.println("不包含");
}
}
js 判断字符串是否包含某字符串,String对象中查找子字符,indexOf
var Cts
= "bblText";
if(Cts.indexOf("Text")
0 )
{
alert('Cts中包含Text字符串');
}
indexOf用法:
返回 String 对象内第一次出现子字符串的字符位置。
strObj.indexOf(subString[, startIndex])
参数
strObj
必选项。String 对象或文字。
subString
必选项。要在 String 对象中查找的子字符串。
starIndex
可选项。该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。
说明
indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。
如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。
从左向右执行查找。否则,该方法与 lastIndexOf 相同。
示例
下面的示例说明了 indexOf 方法的用法。
function IndexDemo(str2){
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = str1.indexOf(str2);
return(s);
}
对于JavaScript的indexOf忽略大小写
javascript中indexOf函数方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。
indexOf函数是从左向右执行查找。否则,该方法与 lastIndexOf 相同。
下面的示例说明了indexOf函数方法的用法。
function IndexDemo(str2){
var str1
= "BABEBIBOBUBABEBIBOBU"
var s
= str1.indexOf(str2);
return(s);
}
java截取相关
1、length() 字符串的长度
例:char chars[]={'a','b'.'c'};
String s=new String(chars);
int len=s.length();
2、charAt() 截取一个字符
例:char ch;
ch="abc".charAt(1); 返回'b'
3、getChars() 截取多个字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart指定了子串开始字符的下标,sourceEnd指定了子串结束后的下一个字符的下标。因此,子串包含从sourceStart到sourceEnd-1的字符。接收字符的数组由target指定,target中开始复制子串的下标值是targetStart。
例:String s="this is a demo of the getChars method.";
char buf[]=new char[20];
s.getChars(10,14,buf,0);
4、getBytes()
替代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()。
5、toCharArray()
6、equals()和equalsIgnoreCase() 比较两个字符串
7、regionMatches() 用于比较一个字符串中特定区域与另一特定区域,它有一个重载的形式允许在比较中忽略大小写。
boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars)
8、startsWith()和endsWith()
startsWith()方法决定是否以特定字符串开始,endWith()方法决定是否以特定字符串结束
9、equals()和==
equals()方法比较字符串对象中的字符,==运算符比较两个对象是否引用同一实例。
例:String s1="Hello";
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false
10、compareTo()和compareToIgnoreCase() 比较字符串
11、indexOf()和lastIndexOf()
indexOf() 查找字符或者子串第一次出现的地方。
lastIndexOf() 查找字符或者子串是后一次出现的地方。
12、substring()
它有两种形式,第一种是:String substring(int startIndex)
第二种是:String substring(int startIndex,int endIndex)
13、concat() 连接两个字符串
14 、replace() 替换
它有两种形式,第一种形式用一个字符在调用字符串中所有出现某个字符的地方进行替换,形式如下:
String replace(char original,char replacement)
例如:String s="Hello".replace('l','w');
第二种形式是用一个字符序列替换另一个字符序列,形式如下:
String replace(CharSequence original,CharSequence replacement)
15、trim() 去掉起始和结尾的空格
16、valueOf() 转换为字符串
17、toLowerCase() 转换为小写
18、toUpperCase() 转换为大写
19、StringBuffer构造函数
StringBuffer定义了三个构造函数:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
(1)、length()和capacity()
一个StringBuffer当前长度可通过length()方法得到,而整个可分配空间通过capacity()方法得到。
(2)、ensureCapacity() 设置缓冲区的大小
void ensureCapacity(int capacity)
(3)、setLength() 设置缓冲区的长度
void setLength(int len)
(4)、charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)
(5)、getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
(6)、append() 可把任何类型数据的字符串表示连接到调用的StringBuffer对象的末尾。
例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append("a=").append(a).append("!").toString();
(7)、insert() 插入字符串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index指定将字符串插入到StringBuffer对象中的位置的下标。
(8)、reverse() 颠倒StringBuffer对象中的字符
StringBuffer reverse()
(9)、delete()和deleteCharAt() 删除字符
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)
(10)、replace() 替换
StringBuffer replace(int startIndex,int endIndex,String str)
(11)、substring() 截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)
java 如何判断字符串编码格式
java可供判断某字符串是什么编码的一行代码
System.out.println("中文");
System.out.println("中文".getBytes());
System.out.println("中文".getBytes("GB2312"));
System.out.println("中文".getBytes("ISO8859_1"));
System.out.println(new String("中文".getBytes()));
System.out.println(new String("中文".getBytes(), "GB2312"));
System.out.println(new String("中文".getBytes(), "ISO8859_1"));
System.out.println(new String("中文".getBytes("GB2312")));
System.out.println(new String("中文".getBytes("GB2312"), "GB2312"));
System.out.println(new String("中文".getBytes("GB2312"), "ISO8859_1"));
System.out.println(new String("中文".getBytes("ISO8859_1")));
System.out.println(new String("中文".getBytes("ISO8859_1"), "GB2312"));
System.out.println(new String("中文".getBytes("ISO8859_1"), "ISO8859_1"));
eg:判断当前字符串的编码格式。
//判断当前字符串的编码格式
if(destination.equals(new String(destination.getBytes("iso8859-1"), "iso8859-1")))
{
destination=new String(destination.getBytes("iso8859-1"),"utf-8");
}
java怎么判断输入的字符串是否为时间格式
通过正则表达式判断。代码如下:
public boolean isDate(String date) {
String path="\\d{4}-\\d{2}-\\d{2}";//定义匹配规则
Pattern p=Pattern.compile(path);//实例化Pattern
Matcher m=p.matcher(date);//验证字符串内容是否合法
if(m.matches()){
return true;
}
return false;
}
技术问题:java中如何判断字符串内容是否是一种编码格式
可以看下java.nio.charset.Charset这个类,这个类里面有个静态方法:
public static boolean isSupported(String charsetName)通知是否支持指定的 charset。
参数:
charsetName - 请求的 charset 名称;可能是规范名称或别名
返回:
当且仅当当前 Java 虚拟机支持指定的 charset 时才返回 true
抛出:
IllegalCharsetNameException - 如果给定的 charset 名称是非法的
IllegalArgumentException - 如果给定的 charsetName 为 null
拿这个静态方法判断并捕捉异常,如果是true那就是正确的,否则就是false
怎么 知道 java字符串 编码格式
这样的测试方法是不正确的。getBytes(charset)是解码,new
String(byte[],
charset)是编码。new
String(str.getBytes(charset),charset)是解码再编码,无论charset是什么编码格式,返回的字符串内容原始str是一致,因此equals方法都是返回true,达不到测试字符串编码格式的目的。个人观点:正确的测试姿势应该是这样的:
String charset ="xxx"; //假定编码格式
String str = "中文";
boolean flag = str.equals(new String(str.getBytes(),charset));flag为true则表明str的编码格式是假定的编码格式。其中说明str.getBytes()该方法就是按其自身编码格式去解码。其自身编码格式跟你的操作系统编码格式或你使用的IDE设置的文件的Text
file
encoding有关。
java判断字符串格式的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java判断字符串格式大小、java判断字符串格式的信息别忘了在本站进行查找喔。