melandragem
melandragem

Reputation: 47

Replacing String started with some characters android

I want to ask question.

I have a dynamic ocr result like "Prosdfad" or "Pro324sd". And so I want to replace the string to be "Protein". I searched on this site but I haven't found it. Is there any turorial?

Upvotes: 0

Views: 51

Answers (1)

keyser
keyser

Reputation: 19189

Something like:

if(str.startsWith("Pro")) {
    str = "Protein";
}

Where str is your String object.

However, note that it's case-sensitive, so this won't match e.g. "prosdfad". And you might want to consider doing a startsWith check further ahead instead of reassigning the String, if the part to be replaced contains some useful information.

Here's the String documentation. It has lots of useful methods such as startsWith, toLowerCase, matches, to name a few.

Upvotes: 1

Related Questions