String[] names = {"boby", "angela", "Andy" ,"dlei", "caocao", "Babo", "jack", "Cici"}; //1.要求忽略首字符大小写进行排序。
时间: 2023-10-11 09:08:11 浏览: 188
首先,您可以使用`Arrays.sort()`方法对字符串数组进行排序,并使用自定义的比较器来忽略首字符的大小写。以下是实现此功能的代码示例:
```java
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
String[] names = {"boby", "angela", "Andy" ,"dlei", "caocao", "Babo", "jack", "Cici"};
Arrays.sort(names, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.substring(1).compareToIgnoreCase(s2.substring(1));
}
});
System.out.println(Arrays.toString(names));
}
}
```
这将输出排序后的数组:
```
[angela, Andy, Babo, boby, caocao, Cici, dlei, jack]
```
相关问题
import requests from lxml import etree resp = requests.get(url,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.37'}) print(resp.text) xp = etree.HTML(resp.text) img_urls = xp.xpath('//boby/div/section/div/div/div/section/figure/a/img/@src') img_names =xp.xpath('//boby/div/section/div/div/div/section/figure/a/img/@alt') for u,n in zip(img_urls,img_names): print(f'图片名;{n} 地址;{u}') img_resp = requests.get(u,headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.37'}) with open(f'./venv//img_f/{n}.jpg','wb') as f: f.write(img_resp.content)
根据您提供的代码,我看到您爬取的是图片地址并保存为本地图片。但是,您的代码中似乎缺少一些标签。具体来说,'boby' 应该是 'body',另外您的xpath路径可能也有问题。
您可以尝试如下的代码:
```python
import requests
from lxml import etree
url = 'https://example.com'
resp = requests.get(url,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.37'})
#print(resp.text)
xp = etree.HTML(resp.text)
img_urls = xp.xpath('//body//div//section//div//div//div//section//figure//a//img/@src')
img_names = xp.xpath('//body//div//section//div//div//div//section//figure//a//img/@alt')
for u,n in zip(img_urls, img_names):
print(f'图片名:{n} 地址:{u}')
img_resp = requests.get(u, headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.37'})
with open(f'./venv/img_f/{n}.jpg', 'wb') as f:
f.write(img_resp.content)
```
这个代码会爬取指定网站的图片地址,并将其下载到本地文件夹中。希望这个代码对您有所帮助。
const express = require('express'); const bodyParser =require("body-parser"); const app = express(); //express ->app //NextHandleFunction =(req,res,next) =>{} app.use( bodyParser.urlencoded({ extended:false, }) ); app.use((req,res,next) =>{ console.log(1); req.body="123"; res.send("你好123") }); const port =3000; //请求 GET POST PUT DELETE app.get('/hello', (req, res) => { //res 响应 //req 请求 console.log(req.query); console.log(req.a); console.log(req.boby); console.log(req.params); console.log("我被请求到了"); res.send("你好"); });一行一行翻译
这是一个使用 Express 框架开发的应用程序,引入并使用了两个模块:express 和 body-parser。创建了一个 Express 应用程序(app)并声明了一个中间件函数(NextHandleFunction),使用了 bodyParser 中间件对请求体进行解析,打印输出了数字1,并将请求体数据设为字符串"123",然后响应客户端"你好123"。使用了端口号3000启动了服务程序。定义了一个路由处理程序,当请求路径为"/hello"时,打印输出了请求参数 req.query、请求属性 req.a、请求体内容 req.body、请求参数 req.params,并响应客户端"你好"。
阅读全文