0

I want to replace one word in the String by using substring. But it seen didn't work.

for example: The string is 0000 , and I want to replace the first word from 0 to 1. It should be 1000. but it doesn't.

The code is like the following

String WorkStatus = "0000";
if(WorkStatus.substring(0, 1).matches("0"))
{
    WorkStatus.substring(0, 1).replace("0", "1");
    Log.d(TAG, "WorkStatus.substring(0, 1) = " + WorkStatus.substring(0, 1) + "\n");
    Log.d(TAG, "WorkStatus = " + WorkStatus + "\n");
}

It didn't work , the string always show 0000. And what I want is "1000"

Do I missing something ?

2
  • 1
    String.replace() returns a new String object, it doesn't modify the original. Commented Aug 20, 2014 at 8:36
  • You should start variable names with a lowercase letter. See Java naming conventions, page 16. Commented Aug 25, 2014 at 17:43

4 Answers 4

4

use this

String WorkStatus = "0000";
//You use matches, while you might as well use equals
if (WorkStatus.substring(0, 1).equals("0")) { 
    //reassign workstatus to workstatus where the first entry is a '1' + the last three chars "000"
    WorkStatus = WorkStatus.substring(0, 1).replace("0", "1") + WorkStatus.substring(1, WorkStatus.length()); 
    Log.d(TAG, "WorkStatus.substring(0, 1) = " + WorkStatus.substring(0, 1) + "\n");
    Log.d(TAG, "WorkStatus = " + WorkStatus + "\n");
}

You didnt assign the modified string to WorkStatus

Another possibility is converting the string to a char[] and replacing the index, instead of working with substrings.

String WorkStatus = "0000";
char[] chars = WorkStatus.toCharArray();
if (chars[0] == '0') {
    chars[0] = '1';
    WorkStatus = new String(chars);
}

If you want other chars to become 1 instead of zero, alter the chars[0] into chars[index], where index is the index you want to change from 0 to 1

Or, even easier, use a StringBuilder:

int yourIndex = 2; //your index which you want to check for 0 and change to 1
StringBuilder sb = new StringBuilder("0000");
if (sb.charAt(yourIndex) == '0')
    sb.setCharAt(yourIndex, '1');
WorkStatus = sb.toString();
Sign up to request clarification or add additional context in comments.

5 Comments

I have try , but the WorkStatus only show "1" , not "1000".
it returns 1, not 1000
Yes , but what I want is 1000
But it seem work for only first. If I want to replace the second word. Shoud I use the following code ? WorkStatus.substring(0, 1) + WorkStatus.substring(1, 2).replace("0", "1") + WorkStatus.substring(2, WorkStatus.length());
@Martin Your requirement broadens, it seems. I answered your question, but now you generalize your question. However, if you want to change an index of the string, use the second approach, where you alter the chars index which you modify.
1

method replace has a return value of the string after replaced
you shuold resign the result to the String

WorkStatus=WorkStatus.substring(0, 1).replace("0", "1")+ WorkStatus.substring(1, WorkStatus.length();

4 Comments

I have try , but the WorkStatus only show "1" , not "1000".
because you use the method substring this method reture a new string in your case '0'
So...How to replace the only one word in the string ?
you just wanna replace the first letter of the string?
0

if you asign it to a new variable like the below code, you can get what you needed.

String newWorkStatus=WorkStatus.substring(0, 1).replace("0", "1")+WorkStatus.substring(1);
                  Log.d("LOG", "WorkStatus.substring(0, 1) = " + WorkStatus.substring(0, 1) + "\n");
                  Log.d("LOG", "WorkStatus = " + WorkStatus + "\n");
                  Log.d("LOG", "New WorkStatus = " + newWorkStatus + "\n");

Comments

0

WorkStatus.substring(0, 1).replace("0", "1"); returns 1, you should use StringBuilder instead of String.

My solution:

StringBuilder WorkStatus = new StringBuilder("0000");
int pos = WorkStatus.indexOf("0", 0);
if (pos != -1) {
    WorkStatus.replace(pos, pos + 1, "1");
}
System.out.print(WorkStatus);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.