0

This is a part of a string

test="some text" test2="othertext"

It contains a lot more of similar text with same formating. Each "statment" is separate by empty space How to search by name(test, test2) and replace its values(stuff between "")? in java

I dont know if its clear enough but i dont know how else to explain it

I want to search for "test" and replace its content with something else

replace test="some text" test2="othertext" with something else

Edit: This is a content of a file

  test="some text" test2="othertext"

I read content of that file in a string

Now i want to replace some text with something else

some text is not static it can be anything

8
  • 3
    Please give an example to explain what you want to do. Commented Nov 10, 2012 at 11:55
  • So you want to delete the value of "test" if its present in "test2", right ? Commented Nov 10, 2012 at 12:01
  • just replace content of test with something else Commented Nov 10, 2012 at 12:02
  • your problem is still not clear. that's the time to post some part of code, where you want to implement it. Commented Nov 10, 2012 at 12:03
  • for example change this test="some text" to be this test="something else" Commented Nov 10, 2012 at 12:03

5 Answers 5

2

You can use the replace() method of String, which comes in 3 types and 4 variants:

  • revStr.replace(oldChar, newChar)
  • revStr.replace(target, replacement)
  • revStr.replaceAll(regex, replacement)
  • revStr.replaceFirst(regex, replacement)

Eg:

String myString = "Here is the home of the home of the Stars";
myString = myString.replace("home","heaven");

///////////////////// Edited Part //////////////////////////////////////

String s = "The quick brown fox test =\"jumped over\" the \"lazy\" dog";
String lastStr = new String();
String t = new String();

Pattern pat = Pattern.compile("test\\s*=\\s*\".*\"");
Matcher mat = pat.matcher(s);

        while (mat.find()) {

            // arL.add(mat.group());
            lastStr = mat.group();

        }

Pattern pat1 = Pattern.compile("\".*\"");
        Matcher mat1 = pat1.matcher(lastStr);

        while (mat1.find()) {

            t = mat.replaceAll("test=" + "\"Hello\"");

        }

        System.out.println(t);
Sign up to request clarification or add additional context in comments.

1 Comment

its not that simple. in the example from first post i need to search for test and replace everything between "" after test=
1

So you want to replace every instance of "test" with something else?

Let's say the string name is myString:

myString = myString.replace("test","something else");

Is this what you are looking to do?

1 Comment

no. I have a file containing test="some text" test2="othertext" and other stuff. I read it in as String. now i want to replace part of string that is content of test(between "") with something else
1

I think you are asking that you fetch data from file in the form of string, lets suppose, your string is,

String s = "My name="sahil" and my company="microsoft", also i live in country="india"".

Now you want to replace "sahil" with "mahajan" and "microsoft" with "google".

I have tried experimenting with the string methods to implement this functionality, but didnt find a relavent result. But i could provide you with some methods. You could use regionMatches, indexOf("name=""). But these functions will help you in finding where sahil(suppose) is located. but the replcae function here is difficult to work, because it replaces character sequence, for which you should know the exact character sequence.

Now you might try experimenting with the string methods. It could help.

7 Comments

yes. finally. that's what i want. but sahil can be anything it isnt static
from which type of file, you are getting the data, is it an xml file or any other txt file.?
i know how to read content of that file in a string, just dont know how to search/replace
Is it okay to replcae the text manually or you want it to do programmatically.? The most convenient way to do it, is to first read the file, then show in edit text and then you can manually replace the text which you want.
no, it must be programmatically. that process needs to be done without user interaction
|
1

I haven't tested this, but it should work:

String mFileContents;
private void replaceValue(String name, String newValue) {
    int nameIndex = mFileContents.indexOf(name);
    int equalSignIndex = mFileContents.indexOf("=", nameIndex);
    int oldValueIndex = equalSignIndex + 2;
    int oldValueLength = mFileContents.indexOf("\"", oldValueIndex);
    String oldValue = mFileContents.substring(oldValueIndex, oldValueLength);

    String firstHalf = mFileContents.substring(0, oldValueIndex -1);
    String secondHalf = mFileContents.substring(oldValueIndex);
    secondHalf.replaceFirst(oldValue, newValue);

    mFileContents = firstHalf + secondHalf;
}

1 Comment

this returns original string but without first "
0
String a = "some text";
    a = a.replace("text", "inserted value");
    System.out.print(a);

Try this

2 Comments

test isnt string its part of other string
i dont have a code. i dont know how to start. All i did so far is read a content of a file in String

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.