Check For A Especific Value Inside A String
Solution 1:
is this what you are looking for? not sure...
Input: 2BXX09XX2BYYYY Output: 2BXX0AXX2B2BYYYY
publicclassTemp{
publicstaticvoid main (String args[]) {
String input = convertSting("2BXX09XX2BYYYY");
input = replaceSize(input, "0A");
System.out.println(input);
}
privatestaticString replaceSize(String input, String newSizeVal) {
int sizePosition = 4;
return input.substring(0, sizePosition) + newSizeVal + input.substring(sizePosition + newSizeVal.length(), input.length());
}
publicstaticString convertSting (String input) {
String findString = "2B";
int firstIndex = input.indexOf(findString) + findString.length();
return input.substring(0, firstIndex) + input.substring(firstIndex, input.length()).replace(findString, "2B2B");
}
}
Solution 2:
public static void main(String[] args)
{
Stringstr = "2B05A82BF1";
String strNew = "";
if (str.contains("2B"))
{
strNew = str.replaceFirst("2B", "");
}
int index = 0;
if (strNew.contains("2B"))
{
index = strNew.indexOf("2B");
}
strNew = str.substring(0, index + 4);
strNew = strNew + "2B" + str.substring(index + 4);
System.out.println(strNew);
String tempStr = strNew.substring(2, 4);
int i= Integer.parseInt(tempStr);
//System.out.println(i);
i=i+1;
if(i<10)
{
strNew=strNew.replace(tempStr, "0"+ String.valueOf(i));
}
else
{
strNew=strNew.replace(tempStr, String.valueOf(i));
}
System.out.println(strNew);
}
Solution 3:
If you are taking the string and converting every pair of chars in the related number in one byte (for example 2b0a10
would be transform in [43, 10, 16]
), I think it would be easier to check the byte array than the String one: you just need to search the number 43 and insert another one.
I'd use an ArrayList
to add every byte value, inserting the value 43 (0X2B) twice every time I find one, and then putting the values into a plain array
.
Checking the String is more complex because you have to take into account that 2b02b0 is actually 2b, 02, b0, there is no '2b' value there.
Example:
publicclassExample {
publicstaticbyte[] parseOutput(byte[] output) {
ArrayList<Byte> aux= new ArrayList<Byte>();
aux.add(output[0]); //we know the first value is 2bfor (int i=1;i< output.length;i++) {
aux.add(output[i]);
if (output[i]==0x2b) {
aux.add(output[i]); //added twice
}
}
//we cannot use toArray because autoboxing does not work byte[] result= newbyte[aux.size()];
for (int i=1;i< aux.size();i++) {
result[i]=aux.get(i);
}
return result;
}
publicstaticvoidmain(String[] args) {
byte[] b={43,11,11,43,15};
byte[] result=parseOutput(b);
for (byte data: result) {
System.out.print(Integer.toHexString(data)+" "); // shows "0 b b 2b 2b f"
}
}
}
Solution 4:
well, its your call when to check for the '2B'. Figure out where you can actually do something after finding a '2B'. If u decide to check for '2b' in java you can do so by
String result = "2B0509045AF2FF1F04A0BCF17E";
if(result.contains("2B"))
{
//do something
}
else
{
//do something else
}
Post a Comment for "Check For A Especific Value Inside A String"