Это работает, но вам нужно вставить свою собственную проверку ошибок.
public class StringConcatenate {
public static void main(String[] args){
// Create two arrays to concatenate and one array to hold both
String[] arr1 = new String[]{"s","t","r","i","n","g"};
String[] arr2 = new String[]{"s","t","r","i","n","g"};
String[] arrBoth = new String[arr1.length+arr2.length];
// Copy elements from first array into first part of new array
for(int i = 0; i < arr1.length; i++){
arrBoth[i] = arr1[i];
}
// Copy elements from second array into last part of new array
for(int j = arr1.length;j < arrBoth.length;j++){
arrBoth[j] = arr2[j-arr1.length];
}
// Print result
for(int k = 0; k < arrBoth.length; k++){
System.out.print(arrBoth[k]);
}
// Additional line to make your terminal look better at completion!
System.out.println();
}
}
Это, вероятно, не самый эффективный, но он не зависит ни от чего, кроме собственного API Java.
Функция OutputDebugString сделает это.
пример кода
void CClass::Output(const char* szFormat, ...)
{
char szBuff[1024];
va_list arg;
va_start(arg, szFormat);
_vsnprintf(szBuff, sizeof(szBuff), szFormat, arg);
va_end(arg);
OutputDebugString(szBuff);
}
Если это для вывода отладки, то OutputDebugString это то, что вы хотите. Полезный макрос:
#define DBOUT( s ) \
{ \
std::ostringstream os_; \
os_ << s; \
OutputDebugString( os_.str().c_str() ); \
}
Это позволяет вам говорить такие вещи, как:
DBOUT( "The value of x is " << x );
Вы можете расширить это, используя макросы __ LINE __
и __ FILE __
, чтобы предоставить еще больше информации.
Для в Windows и в области широких символов:
#include <Windows.h>
#include <iostream>
#include <sstream>
#define DBOUT( s ) \
{ \
std::wostringstream os_; \
os_ << s; \
OutputDebugStringW( os_.str().c_str() ); \
}
Используйте функцию OutputDebugString
или макрос TRACE
(MFC), который позволяет делать printf
-стилевое форматирование:
int x = 1;
int y = 16;
float z = 32.0;
TRACE( "This is a TRACE statement\n" );
TRACE( "The value of x is %d\n", x );
TRACE( "x = %d and y = %d\n", x, y );
TRACE( "x = %d and y = %x and z = %f\n", x, y, z );