-2

I need to find the first character of all the lines and remove it using Java.

Input lines:

Z301052023KDE12   0000.062500000 0000.000000000 0000.000000000
Z301052023KDE11   0000.000000000 0000.000000000 0000.000000000

Java code:

    public static void main(String args[])
    {
        

        String inString = "Z302052023CFCL11   0000.000000000 249999.990        0999999990000.000000000 0000.000000000 0000.000000000 0000.000000000\n"
         + "Z302052023CFCL11   0000.000000000 249999.990        0999999990000.000000000 0000.000000000 0000.000000000 0000.000000000" ;
        
        String value = null;

        inString = inString.replaceAll("^.", "");
        System.out.println(inString);
    }

Output:

302052023CFCL11   0000.000000000 249999.990        0999999990000.000000000 0000.000000000 0000.000000000 0000.000000000
Z302052023CFCL11   0000.000000000 249999.990        0999999990000.000000000 0000.000000000 0000.000000000 0000.000000000

As you can see the 2nd line's first character is not removed.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
Nijith
  • 69
  • 6

1 Answers1

1

With Python:

import re

input_lines = '''
Z301052023KDE12 0000.062500000 0000.000000000 0000.000000000
Z301052023KDE11 0000.000000000 0000.000000000 0000.000000000
'''

pattern = '^.'

first_chars = re.findall(pattern, input_lines, flags=re.MULTILINE)
print(first_chars)

Result:

['Z', 'Z']
Andreas Violaris
  • 2,465
  • 5
  • 13
  • 26
blvc3
  • 24
  • 3
  • Hi Andreas, Thank you for the reply. Is the same pattern holds good for Java as well? – Nijith May 02 '23 at 11:27
  • Hi @Andreas, I tried the pattern in Java but only first line's first character is getting selected. – Nijith May 02 '23 at 12:01
  • I didn't actually answer the question, I just edited blvc3's answer. However, since you asked, it can be done like this in Java: `Pattern.compile("^.", Pattern.MULTILINE).matcher("Z301052023KDE12 0000.062500000\nZ301052023KDE11 0000.000000000").results().forEach(match -> System.out.println(match.group()));`. This is a one-liner to be readable as a comment, but you can break down its commands into multiple lines if it's not clear to you. – Andreas Violaris May 02 '23 at 12:48