当前位置:网站首页>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 位置之间的字符串部分。
边栏推荐
- [FPGA tutorial case 27] realize dual port RAM ping-pong structure through Verilog
- C list set object de duplication LINQ de duplication with time de duplication
- [fuel cell] simulation of fuel cell system control strategy based on Simulink
- Tidb learning
- Using everything to clean up junk files
- 拥抱声明式UI
- 高等数学笔记:伍月习题选集
- TiDB 性能优化概述
- PowerDesigner displays comment comments
- mysql优化
猜你喜欢

Usage scenarios and usage of judgment and rounding down in MySQL

Project structure of wechat applet

Tasking 新建 AURIX TC37X demo 工程

TCP/IP 协议

Hightec new aurix tc37x demo project

HighTec 新建 AURIX TC37X demo 工程

【Lipschitz】基于matlab的Lipschitz李氏指数仿真

Freshman task-5

Penetration test 10 - scan web directories (dirb, wfuzz, wpscan, Nikto)
![[unity] interactive double click](/img/28/8c9c3dd9de413ff8e6373ea111b04f.png)
[unity] interactive double click
随机推荐
Fudan micro fmql (domestic zynq) [PS of IAR bare metal development] - non byte aligned access
itext修改pdf文字
Unit UMP Packaging Black Screen issue Summary
MySQL one line to many lines (split according to specific symbols)
Redis 集群面试题
Monitoring and alarm of kubernetes
PHP array_ Filter empty leave 0, false
Overview of CKA core knowledge points
redis 安装
Record a stored procedure to batch modify the table structure
Tidb learning
Constraints on MySQL tables (Basics)
C language dynamic memory development and flexible array
Warriors of the Visual Studio, Assemble! (Visual Studio的勇士们,汇编吧!) 原创 2009年07月12日 19:40:00 标签:汇编 /mic
接口的参数返回封装类Result
[FPGA tutorial case 27] realize dual port RAM ping-pong structure through Verilog
Embrace declarative UI
Hyperlink implementation post submission
Hightec new aurix tc37x demo project
Simple UI funny text conversion Emoji expression wechat applet supports sentence word conversion_ Source code