Mike
Mike

Reputation: 97

How to remove first string and comma from a text file

! I have a text, the content looks like [1,'I am java, and I am happy,......'], I want to remove the first integer and the comma. When I was run the code above, the result start with last comma

I have a text, the content looks like [1,'I am java, and I am happy, I am.....'], I want to remove the first integer and the comma. When I was run the code above, but the result start with last comma: I am......

Upvotes: 2

Views: 1598

Answers (7)

Sivaranjani D
Sivaranjani D

Reputation: 532

Use this following code as:

System.out.println(line.substring(2));

sub string takes the beginning index as a parameter and splits the string from that index to till the end.

Upvotes: 1

Chandra Prakash
Chandra Prakash

Reputation: 791

String input = "[1,'I am java, and I am happy, I am.....']";

//Getting String after first comma

String output = StringUtils.substringAfter(input, ","); System.out.println("Output:"+output);

//replacing commas;

System.out.println("Final o/p:"+StringUtils.replace(output, ",",""));

You can use methods in StringUtils Class for string manipulations. For using StringUtils methods, you need to import apache-commons-lang.jar file. Using this API you can manipulate many String related methods. For more details, you can see the link

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html

Upvotes: 0

Benjamin
Benjamin

Reputation: 2286

Try This code:

      String[] s=line.splite(",");
      String m="";
      for(int i=1;i<s.length;i++)
      {
      String m=m+s[i];
      }
      br.append(m);

Upvotes: 0

Nambi
Nambi

Reputation: 12042

You need to use the indexOfReturns the index within this string of the first occurrence of the specified character, starting the search at the specified index..

lastIndexOf Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.

System.out.print(s.substring(s.indexOf(",")+1));

Upvotes: 2

jmail
jmail

Reputation: 6134

If you only want to remove commas from a String, you can use String.replaceAll(",",""); If you want to replace them by spaces, use String.replaceAll(","," "):

while ((line = br.readLine()) != null) {
    contents.append(line.replaceAll(","," ");
}

Also in your code you seem to split the input, but don't use the result of this operation.

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347194

I'm taking your String literially, but you could use String#replaceFirst, for example...

String text = "[1,'I am java, and I am happy, I am.....']";
text = text.replaceFirst("\\[\\d,", "[");
System.out.println(text);

Which outputs...

['I am java, and I am happy, I am.....']

If you want to update the file, you are either going to have to read all the lines into some kind of List (modifying them as you please) and once finished, write the List back to the file (after you've closed it after reading it).

Alternatively, you could write each updated line to a second file, once you're finished, close both files, delete the first and rename the second back in it's place...

Upvotes: 0

anirudh
anirudh

Reputation: 4176

Note that you are using lastIndexOf(). Use indexOf() to get the first index as shown below.

System.out.println(test.substring(line.indexOf(',')+1));

Upvotes: 1

Related Questions