gpt4 book ai didi

java - 如何从 "int a, b, c = 0, d;"中提取子字符串(变量名称)

转载 作者:行者123 更新时间:2023-12-01 10:19:18 24 4
gpt4 key购买 nike

我的代码可以在文本文件中搜索字符串“int”并从中搜索子字符串(变量名称),但我对子字符串(变量名称)有一些问题

like "int a, b, c = 0, d;"

我希望我的输出是:

  • 变量名称是:a
  • 变量名称为:b
  • 变量名称为:c
  • 变量名称为:d

我可以修改我的代码吗?

这是我的代码。

                //print variable name and position
int k4 = line.indexOf("=",k+1);
int k5 = line.indexOf(";", k+1);
int k6 = line.indexOf(",", k+1);
int cTemp1 = 0;

//Count whitespace before variable name, print variable name and length of variable name
if (k4 != -1){
String temp4 = line.substring(k+3, k4); //Substring variable name
if(k4 != -1){
int varName = line.indexOf(temp4.trim(), k+3);
String total_length = line.substring(k+3, varName);
int wPattern = total_length.length();
System.out.println(" - Whitespace before variable name \"" + temp4.trim() + "\" are: ," + wPattern);
}
System.out.println(" - Variable name type INTEGER at line: " + lineNumber + " is: "+temp4.trim());
System.out.print(" - Length of variable name \"" + temp4.trim() + "\" is: ," + temp4.trim().length() + "\n\n");
varLength_Integer1 += temp4.trim().length();
}
else if(k5 != -1){
String temp5 = line.substring(k+4, k5);
System.out.println(" - Variable name type INTEGER at line: " + lineNumber + " is: "+temp5.trim());
System.out.print(" - Length of variable name \"" + temp5.trim() + "\" is: ," + temp5.trim().length() + "\n\n");
varLength_Integer2 += temp5.trim().length();
}
else if(k6 != -1){
String temp6 = line.substring(k+4, k6);
System.out.println(" - Variable name type INTEGER at line: " + lineNumber + " is: "+temp6.trim());
System.out.print(" - Length of variable name \"" + temp6.trim() + "\" is: ," + temp6.trim().length() + "\n\n");
varLength_Integer3 += temp6.trim().length();
}
avgInteger += (varLength_Integer1 + varLength_Integer2 + varLength_Integer3)/(double)c3;
//END Count whitespace before variable name, print variable name and length of variable name

}

非常感谢。

最佳答案

我想用正则表达式来做到这一点

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParseInt {

public static void main(String[] args) {
String test = "int a, b, c = 0, d;\n\nint e, b, c = 0, d";

String pattern = "int(.*)";
Pattern r = Pattern.compile(pattern);

String patternVar = "([A-Za-z][a-zA-Z_0-9]*)(\\[\\])?";
Pattern rVar = Pattern.compile(patternVar);

String[] linesNewline = test.split("\n");

for (int nLI=0;nLI < linesNewline.length; ++nLI) {
String[] lines = linesNewline[nLI].split(";");
for (int i=0;i < lines.length; ++i) {
Matcher m = r.matcher(lines[i]);
if (m.find()) {
String[] variables = m.group(1).split(",");
for (int ii=0; ii < variables.length; ++ii) {
Matcher mVar = rVar.matcher(variables[ii].trim());
if (mVar.find()) {
System.out.println("Variable is " + mVar.group(1));
}
}
}
}
}
}
}

输出针对输入字符串“int a, b, c = 0, d;\n\nint e, b, c = 0, d”

Variable is a
Variable is b
Variable is c
Variable is d
Variable is e
Variable is b
Variable is c
Variable is d

输出针对输入字符串“int a, a1, b, b1;”

Variable is a
Variable is a1
Variable is b
Variable is b1

输出针对输入字符串“int d[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };”

Variable is d

输出针对输入字符串“int d0 = sc.nextInt(); int d1 = sc.nextInt(); int d2 = sc.nextInt();”

Variable is d0
Variable is d1
Variable is d2

关于java - 如何从 "int a, b, c = 0, d;"中提取子字符串(变量名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35720367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com