java 如何在java中获取字符串值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31800526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get string value in java
提问by dragullar
String Qty1 = "1";
String Qty2 = "2";
String qtyString = "", bString = "", cString = "";
for(int j = 1; j <= 2; j++)
{
qtyString = String.valueOf(("Qty" + j));
System.out.println("qtyString = " + qtyString);
}
output:
输出:
qtyString = Qty1;
qtyString = Qty2;
qtyString = Qty1;
qtyString = Qty2;
I would like to get qtyString = 1, qtyString = 2; I want to print Qty1 and Qty2 value in my for loop. In C#, this code works correctly. I don't know how to get qtyString value as 1, 2 in java.
我想得到 qtyString = 1, qtyString = 2; 我想在我的 for 循环中打印 Qty1 和 Qty2 值。在 C# 中,此代码可以正常工作。我不知道如何在 java 中将 qtyString 值设为 1、2。
采纳答案by Taher Rahgooy
You should use array of strings for this purpose.
为此,您应该使用字符串数组。
String[] Qty = {"1","2"};
for(int j = 0 ; j < 2 ;j++)
{
System.out.println("qtyString = " + Qty[j]);
}
回答by Desmond DAI
String array is needed if you want to print a list of string:
如果要打印字符串列表,则需要字符串数组:
String[] Qty = {"1", "2"};
String qtyString = null;
for (int j = 0; i<=1; j++) {
qtyString = Qty[j];
System.out.println("qtyString = " + qtyString);
}
output:
qtyString = 1
qtyString = 2
回答by Ashish Jaiswal
you can try this for java: static String Qty[] = {"1", "2"} ;
你可以在 java 上试试这个: static String Qty[] = {"1", "2"} ;
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int j = 0 ; j < 2 ;j++)
{
System.out.println("qtyString = " + Qty[j]);
}
}
And For Android:
对于安卓:
String[] Qty = {"1","2"};
for(int j = 0 ; j < 2 ;j++)
{
System.out.println("qtyString = " + Qty[j]);
}
回答by Ashish Jaiswal
Try this examples:
试试这个例子:
With enhanced for-loop and inline array:
使用增强的 for 循环和内联数组:
for(String qty : new String[]{"1", "2"})
System.out.printf("qtyString = %s\n", qty);
With enhanced for-loop and array:
使用增强的 for 循环和数组:
String [] qtys = {"1", "2"};
for(String qty : qtys)
System.out.printf("qtyString = %s\n", qty);
Using for-loop and array:
使用 for 循环和数组:
String [] qty = {"1", "2"};
for(int i = 0; qty.length > i; i ++)
System.out.printf("qtyString = %s\n", qty[i]);
回答by ajb
I don't know for sure what you're trying to do, and you've declared Qty1
twice in your code. Assuming the second one is supposed to be Qty2
, then it looks like you're trying to use string operations to construct a variable name, and get the value of the variable that way.
我不确定您要做什么,并且您Qty1
在代码中声明了两次。假设第二个应该是Qty2
,那么看起来您正在尝试使用字符串操作来构造变量名称,并以这种方式获取变量的值。
You cannot do that in Java.I'm not a C# expert, but I don't think you can do it in C# either (whatever you did that made you say "it works in C#" was most certainly something very different). In both those languages and in all other compiledlanguages, the compiler has to know, at compile time, what variable you're trying to access. You can't do it at runtime. (Technically, in Java and C#, there are ways to do it using reflection, depending on how and where your variables are declared. But you do not want to solve your problem that way.)
你不能在 Java 中做到这一点。我不是 C# 专家,但我认为你也不能在 C# 中做到这一点(无论你做了什么让你说“它在 C# 中有效”肯定是非常不同的东西)。在这些语言和所有其他编译语言中,编译器必须在编译时知道您要访问的变量。您不能在运行时执行此操作。(从技术上讲,在 Java 和 C# 中,有使用反射的方法,这取决于声明变量的方式和位置。但您不想以这种方式解决问题。)
You'll need to learn about maps. Instead of separate variables, declare a Map<String, String>
that maps the namethat you want to associate with a value (Qty1
, Qty2
) with the value (which is also a String
in this case, but could be anything else). See this tutorialfor more information. Your code will look something like
您需要了解地图。而不是单独的变量,声明 aMap<String, String>
将您想要与值 ( , )关联的名称与该值(在本例中也是 a ,但可以是其他任何内容)进行映射。有关更多信息,请参阅本教程。你的代码看起来像Qty1
Qty2
String
Map<String, String> values = new HashMap<>();
values.put("Qty1", "1");
values.put("Qty2", "2");
...
qtyString = values.get("Qty"+j);
(Actually, since your variable name is based on an integer, an array or ArrayList
would work perfectly well too. Using maps is a more general solution that works in other cases where you want names based on something else beside sequential integers.)
(实际上,由于您的变量名称基于整数,因此数组或ArrayList
也可以很好地工作。使用映射是一种更通用的解决方案,适用于您希望名称基于顺序整数以外的其他内容的其他情况。)