当前位置:网站首页>String字符串根据符号进行特殊截取处理
String字符串根据符号进行特殊截取处理
2022-07-17 05:02:00 【小新爱编程】
项目场景:
提示:获取服务器地址然后进行数据处理
现在知道了服务器地址是多少,我们把文件上传到服务器,然后如果要存文件名或者文件后缀,就会引发问题,然后要对字符串进行数据处理和分析,就必须在代码中进行字符串的分析和汇总,然后才能符合我们现有的项目场景。
问题描述:
提示:文件名和后缀的处理
我们得到的数据是这样的
String fileSite = "http://192.168.1.22:8081/imgPortl /ReadImg/181217775991024.jpg";
根据我们得到的数据,发现里面有各种奇怪的符号,如果你用简单的splict肯定是无法实现效果,因为各种符号和文件上传的路径会影响到我们的截取,这个时候就需要想一下别的方法进行数据的分析和处理。
原因分析:
提示:string字符串处理可以解决这个问题
我们用字符串的地位和截图然后快速查找数据进行处理,就可以实现我们所需要的效果。
substring()的作用就是截取父字符串的某一部分
第一个参数int为开始的索引,对应String数字中的开始位置,
第二个参数是截止的索引位置,对应String中的结束位置
1、取得的字符串长度为:endIndex - beginIndex;
2、从beginIndex开始取,到endIndex结束,从0开始数,其中不包括endIndex位置的字符
这样的数据我们才可以进行下一步的分析,substring的功能在此刻表现出前所未有的强大,目前个人只想到这个处理会更好的解决,如果有更好的办法,欢迎交流。
注意:
1)、substring(a,b)中的参数a其值可以从索引值0开始,
当a等于0时,则表示其从字符串的第一个字符开始算起,
也就是其子字符串是包含第一个字符的,当然,你也可以从后续的其他字符开始;
2)、substring(a,b)中的参数b,其值最大可以为 父字符串的长度,
但并不包含索引值为b的那个字符
解决方案:
// 文件地址
String fileSite = "http://192.168.1.22:8081/imgPortl/ReadImg/181217775991024.jpg";
// 文件名称
String oriName = fileSite.substring(fileSite.lastIndexOf("/")+1);
// 文件名
String substring = oriName.substring(0, oriName.lastIndexOf("."));
System.out.println(substring); // 输出
总结:
1.xx.substring()括号中带的参数不一样,效果就会有很大的区别,详细如下:
2.xx.substring(0,2)表示取第一个和第二个字符(0,1,2表示第一、二、三个字符,含头不含尾的原则就只包含第一、二个字符),返回一个新的字符串(只包含指定的第一和第二个字符);
3.xx.substring(2)表示去掉前两个字符,返回一个新的字符串(只包含去掉前两个字符后剩下的字符串)
4.此方法中的 beginIndex 表示截取的起始索引,截取的字符串中包括起始索引对应的字符;endIndex 表示结束索引,截取的字符串中不包括结束索引对应的字符,如果不指定 endIndex,则表示截取到目标字符串末尾。该方法用于提取位置 beginIndex 和位置 endIndex 位置之间的字符串部分。
边栏推荐
- 渗透测试 10 --- 扫描 web目录 (dirb、wfuzz、wpscan、nikto)
- Construction and application of knowledge map de (VII): large scale knowledge map pre training
- 数据库与开源的未来
- PowerDesigner displays comment comments
- Redis cluster interview questions
- [fuel cell] simulation of fuel cell system control strategy based on Simulink
- MySQL one line to many lines (split according to specific symbols)
- shardingproxy分库分表实战及同类产品对比
- Multiple connections will be maintained for each provider instance
- 【TA-霜狼_may-《百人计划》】图形2.5 Bump Mapping
猜你喜欢
![[TA frost wolf \u may- hundred people plan] Introduction to basic rendering lighting (I)](/img/3e/6eedbb21db4730d7c40962978d93f0.png)
[TA frost wolf \u may- hundred people plan] Introduction to basic rendering lighting (I)

2022最新版校园跑腿小程序源码

Quickly master Mipi development strategy

Sphinx遇到的问题

C language dynamic memory development and flexible array

负载均衡器ribbon实战

OLTP 负载性能优化实践

Database learning notes (I) retrieval data

TiDB 性能优化概述

shardingproxy分库分表实战及同类产品对比
随机推荐
【FPGA教程案例26】在FPGA中通过verilog来实现小数的基础运算
数据库与开源的未来
C language dynamic memory development and flexible array
itext修改pdf文字
新生任务-5
记录一次存储过程批量修改表结构
Record a stored procedure to batch modify the table structure
赚钱大师小程序【最新版5.9.9】商城/佣金即时提现/分销推广/话费充值/美团饿了么外卖
Emqx pressure test tread pit for your reference
邮箱发送邮件(包含附件,网易、QQ)
Policy mode replaces if else
thinkphp 官网教程
Notes on Advanced Mathematics: a conjecture about the Equivalent Infinitesimal Substitution
OLTP 负载性能优化实践
Monitoring and alarm of kubernetes
Database learning notes (I) retrieval data
Quickly master Mipi development strategy
Hyperlink implementation post submission
加密和解密
服务端接口测试-接口测试的测试点【杭州多测师】【杭州多测师_王sir】