Mac系统 java replace
时间: 2024-09-06 09:08:19 浏览: 60
在Mac系统上,如果你想要使用Java进行字符串替换操作,你可以利用Java的内置`String`类提供的`replace()`方法。这个方法用于在一个字符串中替换所有指定字符或者子串。基本语法如下:
```java
String originalString = "这是一个需要替换的字符串";
String replacement = "替换成的内容";
String modifiedString = originalString.replace("需要替换的子串", replacement);
```
在这个例子中,`originalString.replace("需要替换的子串", replacement)`会返回一个新的字符串,其中所有出现的"需要替换的子串"都被`replacement`所取代。
如果你想处理更复杂的模式匹配,可以使用正则表达式,`replaceAll()`方法就支持这种功能:
```java
String pattern = "[aeiou]"; // 正则表达式,表示匹配任意小写字母
String modifiedString = originalString.replaceAll(pattern, "*"); // 将所有小写字母替换为星号
```
相关问题
微信支付二维码Java代码
以下是微信支付二维码的Java代码示例:
```java
import java.net.URLEncoder;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class WeChatPayQRCode {
private static final String CHARSET_UTF8 = "UTF-8";
private static final String SIGN_TYPE_HMAC_SHA256 = "HMAC-SHA256";
private static final String SIGN_TYPE_MD5 = "MD5";
private static String createSign(SortedMap<String, String> parameters, String key, String signType)
throws Exception {
StringBuilder sb = new StringBuilder();
for (String k : parameters.keySet()) {
String v = parameters.get(k);
if (v != null && !"".equals(v.trim()) && !"sign".equals(k) && !"key".equals(k)) {
sb.append(k).append("=").append(v).append("&");
}
}
sb.append("key=").append(key);
if (SIGN_TYPE_MD5.equals(signType)) {
return MD5(sb.toString()).toUpperCase();
} else if (SIGN_TYPE_HMAC_SHA256.equals(signType)) {
return HMACSHA256(sb.toString(), key);
} else {
throw new Exception(String.format("Invalid sign_type: %s", signType));
}
}
private static String MD5(String data) throws Exception {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(data.getBytes(CHARSET_UTF8));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
}
private static String HMACSHA256(String data, String key) throws Exception {
Mac sha256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(CHARSET_UTF8), "HmacSHA256");
sha256.init(secretKeySpec);
byte[] array = sha256.doFinal(data.getBytes(CHARSET_UTF8));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
}
private static String buildQuery(SortedMap<String, String> parameters) throws Exception {
StringBuilder sb = new StringBuilder();
for (String k : parameters.keySet()) {
String v = parameters.get(k);
if (v != null && !"".equals(v.trim())) {
sb.append(k).append("=").append(URLEncoder.encode(v, CHARSET_UTF8)).append("&");
}
}
return sb.toString().substring(0, sb.length() - 1);
}
public static String generateQRCode(String appId, String mchId, String key, String body, String outTradeNo,
int totalFee, String notifyUrl, String tradeType) throws Exception {
SortedMap<String, String> parameters = new TreeMap<>();
parameters.put("appid", appId);
parameters.put("mch_id", mchId);
parameters.put("nonce_str", java.util.UUID.randomUUID().toString().replace("-", ""));
parameters.put("body", body);
parameters.put("out_trade_no", outTradeNo);
parameters.put("total_fee", String.valueOf(totalFee));
parameters.put("spbill_create_ip", "127.0.0.1");
parameters.put("notify_url", notifyUrl);
parameters.put("trade_type", tradeType);
parameters.put("product_id", outTradeNo); // NATIVE trade_type必填,商品ID
String sign = createSign(parameters, key, SIGN_TYPE_MD5);
parameters.put("sign", sign);
String query = buildQuery(parameters);
String url = String.format("weixin://wxpay/bizpayurl?%s", query);
return url;
}
}
```
使用示例:
```java
String appId = "your_app_id";
String mchId = "your_mch_id";
String key = "your_key";
String body = "test";
String outTradeNo = "123456789";
int totalFee = 100;
String notifyUrl = "http://your.notify.url";
String tradeType = "NATIVE";
String qrCodeUrl = WeChatPayQRCode.generateQRCode(appId, mchId, key, body, outTradeNo, totalFee, notifyUrl, tradeType);
```
注意,为了使用该代码,你需要引入 Apache Commons Codec 依赖包。可以在 Maven 中添加以下代码:
```xml
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
```
Ethernet发送过程java代码
以下是一个简单的Ethernet发送过程的Java代码示例,假设我们要发送的数据为data,源MAC地址为src_mac,目的MAC地址为dst_mac,协议类型为protocol_type,发送的网卡接口为interface_name:
```java
import java.net.*;
public class EthernetSender {
// 发送Ethernet帧
public static void sendEthernetFrame(byte[] data, String srcMac, String dstMac, int protocolType, String interfaceName) throws Exception {
// 创建Socket
NetworkInterface ni = NetworkInterface.getByName(interfaceName);
DatagramSocket sock = new DatagramSocket();
sock.setBroadcast(true);
sock.setNetworkInterface(ni);
// 封装Ethernet帧
byte[] dstMacBytes = hexStringToByteArray(dstMac.replace(":", ""));
byte[] srcMacBytes = hexStringToByteArray(srcMac.replace(":", ""));
byte[] protocolTypeBytes = new byte[2];
protocolTypeBytes[0] = (byte) ((protocolType >> 8) & 0xff);
protocolTypeBytes[1] = (byte) (protocolType & 0xff);
byte[] ethernetFrame = new byte[dstMacBytes.length + srcMacBytes.length + protocolTypeBytes.length + data.length];
System.arraycopy(dstMacBytes, 0, ethernetFrame, 0, dstMacBytes.length);
System.arraycopy(srcMacBytes, 0, ethernetFrame, dstMacBytes.length, srcMacBytes.length);
System.arraycopy(protocolTypeBytes, 0, ethernetFrame, dstMacBytes.length + srcMacBytes.length, protocolTypeBytes.length);
System.arraycopy(data, 0, ethernetFrame, dstMacBytes.length + srcMacBytes.length + protocolTypeBytes.length, data.length);
// 发送Ethernet帧
DatagramPacket packet = new DatagramPacket(ethernetFrame, ethernetFrame.length, InetAddress.getByName("255.255.255.255"), 0);
sock.send(packet);
// 关闭Socket
sock.close();
}
// 将十六进制字符串转换为字节数组
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
```
其中,我们使用了Java的DatagramSocket和NetworkInterface类来进行Socket编程。通过NetworkInterface.getByName()方法获取指定网卡接口的NetworkInterface对象,然后创建DatagramSocket对象并将其与指定网卡接口绑定。封装Ethernet帧的过程与Python的示例代码基本相同,只是需要手动将协议类型转换为网络字节序(大端序)。最后,创建DatagramPacket对象并使用DatagramSocket.send()方法发送Ethernet帧。注意,Java的DatagramSocket类中没有提供设置网卡接口MAC地址的方法,因此无需手动设置MAC地址。
阅读全文