用代码实现判断字符串的开头和结尾

 1         if(str.indexOf("sdk")==0)
 2         {
 3             System.out.println("字符串str是以sdk开头!");
 4         }else
 5         {
 6             System.out.println("字符串str不是以sdk开头!");
 7         }
 8
 9         if(str.substring(str.length()-3).equals("slf"))
10         {
11             System.out.println("字符串str是以slf结尾");
12         }else
13         {
14             System.out.println("字符串str不是以slf结尾");
15
16         }
17         

运行结果:

时间: 2024-12-24 00:51:51

用代码实现判断字符串的开头和结尾的相关文章

java练习用代码实现判断字符串的开头和结尾

String str = "asdfghjkl"; String str1 = "asd"; // 通过首位字母的索引比较 if (str.indexOf("asd") == 0) { System.out.println("true"); } String str2 = "jkl"; if (str.lastIndexOf("jkl") == str.length() - str2.l

判断字符串的开头和结尾

package com.text_1; public class lianxi1111 { public static void main(String[] args) { // TODO 自动生成的方法存根 //代码实现判断字符串的开头和结尾 String str="jnjcubhksbwiowhfkalohafwac"; if (str.indexOf("jnj")==0) { System.out.println("jnj"+"是

Java判断字符串的开头和结尾

1.判断字符串的开头: String str="abcdefabc"; if(str.indexOf("abc")==0) { System.out.println("开头是abc"); } else { System.out.println("开头不是abc"); } 输出结果: 2.判断字符的结尾 String str="abcdefabc"; int end=str.lastIndexOf("

java 判断字符串的开头和结尾,

str ="abcdefbcbcbc";if(str.indexOf("abc")==0)//用 { System.out.println("true"); } else { System.out.println("false"); } if(str.lastIndexOf("bc")==str.length()-2) { System.out.println("true"); } el

13、如何拆分含有多种分隔符的字符串 14、如何判断字符串a是否以字符串b开头或结尾 15、如何调整字符串中文本的格式 16、如何将多个小字符串拼接成一个大的字符串

13.如何拆分含有多种分隔符的字符串 import re s = "23:41:2314\1234#sdf\23;" print(re.split(r'[#:\;]+',s))  14.如何判断字符串a是否以字符串b开头或结尾 import os,stat #找到当前目录下的文件名称,返回list ret = os.listdir('.') print(ret) for x in ret: #endswith传参类型是tuple if x.endswith(('.py','.html'

python通过正则获取字符串指定开头和结尾的中间字符串的代码

下面的代码是关于python通过正则获取字符串指定开头和结尾的中间字符串的代码,应该能对各位朋友有些好处. def GetMiddleStr(content,startStr,endStr): patternStr = r'%s(.+?)%s'%(startStr,endStr) p = re.compile(patternStr,re.IGNORECASE) m= re.match(p,content) if m: return m.group(1) 原文地址:http://blog.51ct

startsWith(),endsWith()的作用,用法,判断字符串a 是不是以字符串b开头或结尾

Java代码 startsWith: if(a.startsWith(b)) //判断字符串a 是不是以字符串b开头. endsWith: if(a.endsWith(b)) //判断字符串a 是不是以字符串b结尾. JAVA例子 1. public class StringDemo{ public class startsWith { public static void main(String args[]){ String s1="this is my startsWith string&

php 字符串 以 开头 以结尾 startWith endWith

From: http://www.shipingzhong.cn/node/1217 //第一个是原串,第二个是 部份串function startWith($str, $needle) { return strpos($str, $needle) === 0; } //第一个是原串,第二个是 部份串 function endWith($haystack, $needle) { $length = strlen($needle);        if($length == 0)      {  

Delphi判断字符串中是否包含汉字,并返回汉字位置

1,函数代码: { 判断字符串是否包含汉字 // judgeStr:要判断的字符串 //posInt:第一个汉字位置 } function TForm2.IsHaveChinese(judgeStr: string; var posInt: integer): boolean; var p: PWideChar; // 要判断的字符 count: integer; // 包含汉字位置 isHave: boolean; // 是否包含汉字返回值 begin isHave := false; //