Concatenate String

General MicroView Support area - You have questions, find your answers here.

Moderators: phalanx, Help@GeekAmmo, marcus@geekammo

Post Reply
AUArduino
Posts: 1
Joined: Tue Jul 29, 2014 6:22 pm

Concatenate String

Post by AUArduino » Tue Jul 29, 2014 6:25 pm

Is there a command to concatenate a string. If not, is there some way to find the length of a string or where it'll end. Hoping there is a way to do it so I don't have to hard code the set cursor command after every word or variable. Thanks.

Help@GeekAmmo
Posts: 41
Joined: Fri Jul 18, 2014 11:45 am

Re: Concatenate String

Post by Help@GeekAmmo » Sat Aug 02, 2014 5:32 pm

Hi @AuArduino,

You can use the command getFontWidth() to return the current font width, then calculate the length of your string, and multiply them to get the total width. Then compare with getLCDWidth() to check if the string fit on the LCD width. Below is the example. Hope this helps.

Cheers
JP

Code: Select all

#include <MicroView.h>

void setup() {
  // put your setup code here, to run once:
  uView.begin();
  uView.clear(PAGE);
}

void loop() {
  // put your main code here, to run repeatedly: 
  int fontWidth = uView.getFontWidth();
  char strTest[]="Hello";
  char strTest2[]="Hello World Test" ;
  
  int strTestLen=strlen(strTest);
  int strTest2Len=strlen(strTest2);
  
  if ((strTestLen*fontWidth)<uView.getLCDWidth()) 
    uView.print("Fit");
  else 
    uView.print("Not Fit");
  
  uView.setCursor(0,10);
  if ((strTest2Len*fontWidth)<uView.getLCDWidth()) 
    uView.print("Fit");
  else 
    uView.print("NotFit");
  
  uView.display();

  while(1){}  
}

scotta
Posts: 45
Joined: Thu Jul 24, 2014 1:31 pm
Location: Toronto, Canada

Re: Concatenate String

Post by scotta » Wed Aug 20, 2014 6:57 pm

JP,
Is your code example correct? I did a test with the 5 x 7 font and found that getFontWidth() returns 5, which doesn't account for the extra pixel space between characters. At least for the 5 x 7 font, I think you have to multiply the string length by (uView.getFontWidth() + 1).

Help@GeekAmmo
Posts: 41
Joined: Fri Jul 18, 2014 11:45 am

Re: Concatenate String

Post by Help@GeekAmmo » Thu Aug 28, 2014 4:21 am

Hi Scott,

In the actual font file, there is no extra pixel space, so getFontWidth() returning 5 is technically correct.

Depending on how a user wish to display the text, if user is using print() then adding 1 to the getFontWidth() will get the desired result, if the user wish to drawChar() manually without extra pixel space, then getFontWidth() without adding 1 will be correct.

So it all depends on the situation. My example only serves as a basis for the explanation.

Post Reply