简体   繁体   English

如何使用java进行子字符串和连接

[英]how to sub-string and concat by using java

Anyone can guide me?任何人都可以指导我吗? Example get 1234 from the user then I store in a variable then substring and concat by adding "-" 12-34 then store in oracle.示例从用户那里获取 1234 然后我存储在一个变量中,然后通过添加“-”12-34 来连接子字符串和连接,然后存储在 oracle 中。 It returns an error.它返回一个错误。

String a;
String b;
      public class concatab{
      public static void main(String[] args) {
            String a1 = a.substring(1,2);
            String b1 = b.substring(3,2);
            System.out.println(ab);
            String c1 = a1 + "-" + b1;
        }
 }

I couldn't quite tell what you were trying to do with your code.我不太清楚你想用你的代码做什么。 However, here's an example that works for reference:但是,这里有一个可供参考的示例:

public class Main {
  static String a = "1234";

  public static void main(String[] args) {
    String a1 = a.substring(0, 2);
    String b1 = a.substring(2);
    String c1 = a1 + "-" + b1;
    System.out.println(c1);
  }
}
  1. Class members need to be inside a class.班级成员需要在班级内。
  2. Calling the substring method on your strings will throw a NullPointerException as Strings a and b are initialized to null在字符串上调用 substring 方法将抛出 NullPointerException,因为字符串 a 和 b 被初始化为 null
  3. You cannot call substring with the start index being greater than the end index.您不能调用起始索引大于结束索引的子字符串。 Doing so will throw an exception这样做会抛出异常
  4. String a and b must be declared static, as they are referenced from a static context.字符串 a 和 b 必须声明为静态,因为它们是从静态上下文引用的。

Remember, the compiler reads code line by line.请记住,编译器逐行读取代码。

Your code is a set of instructions for the computer to carry out.您的代码是一组供计算机执行的指令。

Try to keep that in mind when creating code.在创建代码时尽量记住这一点。 You tried to concatenate after you printed.您在打印后尝试连接。 Therefore the computer will print the concatenation before you concatenate your intended string.因此,计算机将在您连接预期字符串之前打印连接。

And if you're trying to get any sort of user input, you have to prompt the user.如果你想获得任何类型的用户输入,你必须提示用户。 You can do that by using the Scanner class.您可以通过使用 Scanner 类来做到这一点。 But for the sake of simplicity, let's manually enter the number ourselves.但为了简单起见,让我们自己手动输入数字。

First thing, your variables aren't declared in any scope.首先,您的变量未在任何范围内声明。 They must be declared, at least within the curly braces of the class.它们必须被声明,至少在类的花括号内。

I'm not sure if you have any other methods, but your variables aren't initialized.我不确定您是否还有其他方法,但您的变量未初始化。 In consequence you're trying to find the substring for a string that doesn't exist.因此,您试图为不存在的字符串查找子字符串。

So, let's look at the code and go from there:因此,让我们看一下代码并从那里开始:

public class ConcatAB{
public static void main(String[] args) {
    String a = "0123";
    String b = "123456";
        String a1 = a.substring(1,3);
        String b1 = b.substring(2,4);
        String c1 = a1 + "-" + b1;
        System.out.println(c1);
    }

} }

Strings are arrays of characters.字符串是字符数组。 And because a string is an array, it's index begins at 0. And the way substrings work, they cut from the index, not the element.并且因为字符串是一个数组,它的索引从 0 开始。子字符串的工作方式是从索引中切割,而不是从元素中切割。

When we get the substring and assign its value to string a1, we cut a from (0,3) as opposed to (1,2).当我们获得子字符串并将其值赋给字符串 a1 时,我们从 (0,3) 中切出 a,而不是 (1,2)。 We use 0 and 3 because the cut includes the first number in the parenthesis and cuts the number before the last number in the parentheses.我们使用 0 和 3,因为切割包括括号中的第一个数字,并切割括号中最后一个数字之前的数字。 Same goes for string b1.字符串 b1 也是如此。

Now, when concatenating, we don't even need to assign it a variable we could just print it.现在,当连接时,我们甚至不需要为它分配一个变量,我们可以打印它。 Like:喜欢:

System.out.println(a1 + "-" + b1);

But I'm not to sure if you want to save that string for another purpose.但我不确定您是否想为其他目的保存该字符串。

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java从字符串“automation130214141113(订单#2882649)”中提取子字符串“2882649” - How to extract the sub-string "2882649" from the string "automation130214141113 (order # 2882649)" using Java 如何在java eclipse中使用iText在Pdf中打印字符串时使特定的子字符串变粗? - How to make a particular sub-string Bold while printing a string in Pdf using iText in java eclipse? 如何改进此Java代码以在字符串中查找子字符串? - How can this Java code be improved to find sub-string in a string? 如何使用正则表达式计算所有匹配的子字符串? - How to count all the matching sub-string using regex? 查找和删除Java中的子字符串 - Find and Delete a Sub-String in Java Java用另一个子字符串(值)问题替换子字符串(模式) - Java Replace sub-string (pattern) by another sub-string (value) issue 使用正则表达式在 Java 中的某些特定子字符串之后拆分字符串 - Split a string after some specific sub-string in Java using regex 在 java 中使用正则表达式提取两个特定单词之间的子字符串 - Extract sub-string between two certain words using regex in java 从JAVA中具有特定模式的String获取子字符串 - Get Sub-string from String with specific Pattern in JAVA 从 Java 中的字符串中提取 xml 子字符串 - Extract xml sub-string from a string in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM